Skip to content

Commit

Permalink
fix: manual URL Del call not working
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnvrm committed Mar 12, 2024
1 parent a2d7f0b commit 3adadad
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
26 changes: 15 additions & 11 deletions fastcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func (f *FastCache) Cached(h fastglue.FastRequestHandler, o *Options, group stri
o.Compression.MinLength = 500
}

uri := f.makeURI(r, o)
hashedURI := hash(f.makeURI(r, o))

// Fetch etag + cached bytes from the store.
blob, err := f.s.Get(namespace, group, uri)
blob, err := f.s.Get(namespace, group, hashedURI)
if err != nil {
o.Logger.Printf("error reading cache: %v", err)
}
Expand Down Expand Up @@ -236,17 +236,23 @@ func (f *FastCache) ClearGroup(h fastglue.FastRequestHandler, o *Options, groups

// Del deletes the cache for a single URI in a namespace->group.
func (f *FastCache) Del(namespace, group, uri string) error {
return f.s.Del(namespace, group, uri)
return f.s.Del(namespace, group, hash(uri))
}

// DelGroup deletes all cached URIs under a group.
func (f *FastCache) DelGroup(namespace string, group ...string) error {
return f.s.DelGroup(namespace, group...)
}

func (f *FastCache) makeURI(r *fastglue.Request, o *Options) string {
var hash [16]byte
// hash returns the md5 hash of a string.
func hash(b string) string {
var hash [16]byte = md5.Sum([]byte(b))

return hex.EncodeToString(hash[:])
}

// makeURI returns the URI to be used as the cache key.
func (f *FastCache) makeURI(r *fastglue.Request, o *Options) string {
// lexicographically sort the query string.
r.RequestCtx.QueryArgs().Sort(func(x, y []byte) int {
return bytes.Compare(x, y)
Expand Down Expand Up @@ -274,12 +280,10 @@ func (f *FastCache) makeURI(r *fastglue.Request, o *Options) string {
fasthttp.ReleaseURI(uriRaw)
}

hash = md5.Sum(id)
} else {
hash = md5.Sum(r.RequestCtx.URI().Path())
return string(id)
}

return hex.EncodeToString(hash[:])
return string(r.RequestCtx.URI().Path())
}

// cache caches a response body.
Expand All @@ -295,7 +299,7 @@ func (f *FastCache) cache(r *fastglue.Request, namespace, group string, o *Optio
}

// Write cache to the store (etag, content type, response body).
uri := f.makeURI(r, o)
hashedURI := hash(f.makeURI(r, o))

var blob []byte
if !o.NoBlob {
Expand All @@ -319,7 +323,7 @@ func (f *FastCache) cache(r *fastglue.Request, namespace, group string, o *Optio
}
}

err := f.s.Put(namespace, group, uri, item, o.TTL)
err := f.s.Put(namespace, group, hashedURI, item, o.TTL)
if err != nil {
return fmt.Errorf("error writing cache to store: %v", err)
}
Expand Down
44 changes: 40 additions & 4 deletions fastcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
var (
srv = fastglue.NewGlue()

fc *fastcache.FastCache

content = []byte("this is the reasonbly long test content that may be compressed")
)

Expand Down Expand Up @@ -113,12 +115,12 @@ func init() {
}
},
}

fc = fastcache.New(cachestore.New("CACHE:", redis.NewClient(&redis.Options{
Addr: rd.Addr(),
})))
)

fc = fastcache.New(cachestore.New("CACHE:", redis.NewClient(&redis.Options{
Addr: rd.Addr(),
})))

// Handlers.
srv.Before(func(r *fastglue.Request) *fastglue.Request {
r.RequestCtx.SetUserValue(namespaceKey, "test")
Expand Down Expand Up @@ -271,6 +273,40 @@ func TestCache(t *testing.T) {
}
}

func TestCacheDelete(t *testing.T) {
// First request should be 200.
r, b := getReq(srvRoot+"/cached", "", false, t)
if r.StatusCode != 200 {
t.Fatalf("expected 200 but got %v", r.StatusCode)
}
if !bytes.Equal(b, content) {
t.Fatalf("expected 'ok' in body but got %v", b)
}

// Second should be 304.
r, b = getReq(srvRoot+"/cached", r.Header.Get("Etag"), false, t)
if r.StatusCode != 304 {
t.Fatalf("expected 304 but got '%v'", r.StatusCode)
}
if !bytes.Equal(b, []byte("")) {
t.Fatalf("expected empty cached body but got '%v'", b)
}

err := fc.Del(namespaceKey, group, "/cached")
if err != nil {
t.Fatalf("expected nil but got %v", err)
}

// New request should be 200, since the cache is deleted.
r, b = getReq(srvRoot+"/cached", "", false, t)
if r.StatusCode != 200 {
t.Fatalf("expected 200 but got %v", r.StatusCode)
}
if !bytes.Equal(b, content) {
t.Fatalf("expected 'ok' in body but got %v", b)
}
}

func TestQueryString(t *testing.T) {
// First request should be 200.
r, b := getReq(srvRoot+"/include-qs?foo=bar", "", false, t)
Expand Down

0 comments on commit 3adadad

Please sign in to comment.