aboutsummaryrefslogtreecommitdiff
path: root/cache/cache.go
diff options
context:
space:
mode:
authorjackyzha0 <[email protected]>2020-05-09 14:28:24 -0700
committerjackyzha0 <[email protected]>2020-05-09 14:28:24 -0700
commit35ce1bff3496c67cda124aea3c36bc5b66b1e4e6 (patch)
tree6dedc5d47077ff9831f2e944bc83dd957eb3f894 /cache/cache.go
parentMerge pull request #1 from jackyzha0/hash (diff)
downloadctrl-v-35ce1bff3496c67cda124aea3c36bc5b66b1e4e6.tar.xz
ctrl-v-35ce1bff3496c67cda124aea3c36bc5b66b1e4e6.zip
working mongo doc add
Diffstat (limited to 'cache/cache.go')
-rw-r--r--cache/cache.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/cache/cache.go b/cache/cache.go
new file mode 100644
index 0000000..da27939
--- /dev/null
+++ b/cache/cache.go
@@ -0,0 +1,39 @@
+package cache
+
+import (
+ "sync"
+
+ "github.com/jackyzha0/ctrl-v/db"
+)
+
+type Cache struct {
+ m map[string]db.Paste
+ lock sync.RWMutex
+}
+
+func New() *Cache {
+ return &Cache{
+ m: map[string]db.Paste{},
+ }
+}
+
+func (c *Cache) Get(hash string) (db.Paste, error) {
+ c.lock.RLock()
+ defer c.lock.RUnlock()
+
+ // check if hash in cache
+ if v, ok := c.m[hash]; ok {
+ return v, nil
+ }
+
+ // if it doesnt, lookup from db
+ p, err := db.Lookup(hash)
+ c.add(p)
+ return p, err
+}
+
+func (c *Cache) add(p db.Paste) {
+ c.lock.Lock()
+ c.m[p.Hash] = p
+ c.lock.Unlock()
+}