diff options
| author | Ryan Mehri <[email protected]> | 2020-05-09 21:17:40 -0600 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-05-09 21:17:40 -0600 |
| commit | 14e120bc3fa4da442af0063c1a00852e67fbc6a2 (patch) | |
| tree | 9258832576f9c1bcad6126cf76ef8f7f6a128e82 /backend/cache | |
| parent | Merge pull request #3 from jackyzha0/doc-expiry (diff) | |
| parent | folder refactor (diff) | |
| download | ctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.tar.xz ctrl-v-14e120bc3fa4da442af0063c1a00852e67fbc6a2.zip | |
Merge pull request #4 from jackyzha0/folder-refactor
folder refactor
Diffstat (limited to 'backend/cache')
| -rw-r--r-- | backend/cache/cache.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/backend/cache/cache.go b/backend/cache/cache.go new file mode 100644 index 0000000..bac7ea8 --- /dev/null +++ b/backend/cache/cache.go @@ -0,0 +1,44 @@ +package cache + +import ( + "sync" + + "github.com/jackyzha0/ctrl-v/db" +) + +type Cache struct { + m map[string]db.Paste + lock sync.RWMutex +} + +var C *Cache + +func init() { + C = &Cache{ + m: map[string]db.Paste{}, + } +} + +func (c *Cache) Get(hash string) (db.Paste, error) { + c.lock.RLock() + + // check if hash in cache + v, ok := c.m[hash] + c.lock.RUnlock() + + if 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() + defer c.lock.Unlock() + + c.m[p.Hash] = p +} |