diff options
| -rw-r--r-- | backend/cache/cache.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/backend/cache/cache.go b/backend/cache/cache.go index 43e615a..e075a05 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -2,8 +2,10 @@ package cache import ( "errors" - "github.com/jackyzha0/ctrl-v/security" "sync" + "time" + + "github.com/jackyzha0/ctrl-v/security" "github.com/jackyzha0/ctrl-v/db" ) @@ -43,6 +45,12 @@ func (c *Cache) Get(hash, userPassword string) (db.Paste, error) { c.add(p) } + // check if expired + if time.Now().After(p.Expiry) { + c.evict(p) + return db.Paste{}, PasteNotFound + } + // if there is a password, check the provided one against it if p.Password != "" { // if passwords do not match, the user is unauthorized @@ -73,3 +81,10 @@ func (c *Cache) add(p db.Paste) { c.m[p.Hash] = p } + +func (c *Cache) evict(p db.Paste) { + c.lock.Lock() + defer c.lock.Unlock() + + delete(c.m, p.Hash) +} |