diff options
| author | jackyzha0 <[email protected]> | 2020-06-07 16:59:08 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-06-07 16:59:08 -0700 |
| commit | 168cb83a661764fad9f62b5ce2ec492d99da817b (patch) | |
| tree | 810bf92d547e7e9040febab4843e88b6aff17fe5 /backend/cache | |
| parent | makefile update (diff) | |
| download | ctrl-v-168cb83a661764fad9f62b5ce2ec492d99da817b.tar.xz ctrl-v-168cb83a661764fad9f62b5ce2ec492d99da817b.zip | |
fix cache not invalidating expired pastes
Diffstat (limited to 'backend/cache')
| -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) +} |