Add caching
This commit is contained in:
parent
f166ffab3c
commit
fe9b4b5f5f
4 changed files with 36 additions and 21 deletions
|
|
@ -8,11 +8,9 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func redirectHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
link, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -21,7 +19,7 @@ func redirectHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.Redirect(http.StatusSeeOther, link.RedirectURL.String())
|
||||
}
|
||||
|
||||
func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func creationHandler(c echo.Context, serv *PgService) error {
|
||||
var model CreationModel
|
||||
if err := json.NewDecoder(c.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
|
|
@ -32,8 +30,6 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL.")
|
||||
}
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err = serv.Create(entity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -41,11 +37,9 @@ func creationHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
||||
func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func retrievalByIdHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
l, err := serv.GetById(linkId)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -54,7 +48,7 @@ func retrievalByIdHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.JSON(http.StatusOK, MapEntityToModel(l))
|
||||
}
|
||||
|
||||
func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func allRetrievalHandler(c echo.Context, serv *PgService) error {
|
||||
limit := 20
|
||||
if v := c.QueryParam("limit"); v != "" {
|
||||
num, err := strconv.Atoi(v)
|
||||
|
|
@ -75,8 +69,6 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
offset = num
|
||||
}
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
links, err := serv.GetAll(limit, offset)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -90,11 +82,9 @@ func allRetrievalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
return c.JSON(http.StatusOK, models)
|
||||
}
|
||||
|
||||
func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
||||
func removalHandler(c echo.Context, serv *PgService) error {
|
||||
linkId := c.Param("id")
|
||||
|
||||
serv := PgService{rep: PgRepository{pool: pool}}
|
||||
|
||||
if err := serv.DeleteById(linkId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -103,25 +93,27 @@ func removalHandler(c echo.Context, pool *pgxpool.Pool) error {
|
|||
}
|
||||
|
||||
func AddHandlers(s *echo.Echo, pool *pgxpool.Pool) {
|
||||
serv := NewPgService(PgRepository{pool: pool})
|
||||
|
||||
gr := s.Group("/links")
|
||||
|
||||
gr.POST("", func(ctx echo.Context) error {
|
||||
return creationHandler(ctx, pool)
|
||||
return creationHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.GET("", func(ctx echo.Context) error {
|
||||
return allRetrievalHandler(ctx, pool)
|
||||
return allRetrievalHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.GET("/:id", func(ctx echo.Context) error {
|
||||
return retrievalByIdHandler(ctx, pool)
|
||||
return retrievalByIdHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
gr.DELETE("/:id", func(ctx echo.Context) error {
|
||||
return removalHandler(ctx, pool)
|
||||
return removalHandler(ctx, &serv)
|
||||
})
|
||||
|
||||
s.GET("/:id", func(ctx echo.Context) error {
|
||||
return redirectHandler(ctx, pool)
|
||||
return redirectHandler(ctx, &serv)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ package link
|
|||
|
||||
import (
|
||||
apperrors "cgnolink/errors"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
|
|
@ -12,7 +14,15 @@ type Service interface {
|
|||
}
|
||||
|
||||
type PgService struct {
|
||||
rep PgRepository
|
||||
rep PgRepository
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewPgService(rep PgRepository) PgService {
|
||||
return PgService{
|
||||
rep: rep,
|
||||
cache: cache.New(1*time.Hour, 90*time.Minute),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PgService) Create(link *Link) error {
|
||||
|
|
@ -33,6 +43,12 @@ func (s *PgService) Create(link *Link) error {
|
|||
}
|
||||
|
||||
func (s *PgService) GetById(id string) (*Link, error) {
|
||||
if v, found := s.cache.Get(id); found {
|
||||
if link, ok := v.(*Link); ok {
|
||||
return link, nil
|
||||
}
|
||||
}
|
||||
|
||||
link, err := s.rep.FindById(id)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
|
|
@ -42,6 +58,8 @@ func (s *PgService) GetById(id string) (*Link, error) {
|
|||
return nil, apperrors.NotFoundError{Message: "Link with given ID was not found."}
|
||||
}
|
||||
|
||||
s.cache.Set(id, link, 0)
|
||||
|
||||
return link, nil
|
||||
}
|
||||
|
||||
|
|
@ -55,6 +73,8 @@ func (s *PgService) GetAll(limit int, offset int) (Links, error) {
|
|||
}
|
||||
|
||||
func (s *PgService) DeleteById(id string) error {
|
||||
s.cache.Delete(id)
|
||||
|
||||
if err := s.rep.DeleteById(id); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue