From 168cb83a661764fad9f62b5ce2ec492d99da817b Mon Sep 17 00:00:00 2001 From: jackyzha0 Date: Sun, 7 Jun 2020 16:59:08 -0700 Subject: fix cache not invalidating expired pastes --- backend/cache/cache.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'backend/cache') 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) +} -- cgit v1.2.3