aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Mehri <[email protected]>2020-06-07 18:03:04 -0600
committerGitHub <[email protected]>2020-06-07 18:03:04 -0600
commit5fc7053b38d20eb40d69a08b15a428aae85a9ee7 (patch)
tree810bf92d547e7e9040febab4843e88b6aff17fe5
parentMerge pull request #49 from jackyzha0/fix-makefile (diff)
parentfix cache not invalidating expired pastes (diff)
downloadctrl-v-5fc7053b38d20eb40d69a08b15a428aae85a9ee7.tar.xz
ctrl-v-5fc7053b38d20eb40d69a08b15a428aae85a9ee7.zip
Merge pull request #50 from jackyzha0/cache-invalidation
fix cache not invalidating expired pastes
-rw-r--r--backend/cache/cache.go17
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)
+}