diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-06-06 19:03:08 -0400 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-06-06 19:03:29 -0400 |
commit | 0c6d8c7fa966ab548a1f25c1c85d45e22eb44770 (patch) | |
tree | 2c8edbb1abeb136f802a12eecd3d5322cde94226 /cache/cache.go | |
parent | fb09c968ee49e2257bcb4edbf954548d5f08ba16 (diff) |
add `cache` package
A simple key-value store.
Diffstat (limited to 'cache/cache.go')
-rw-r--r-- | cache/cache.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..fbc763e --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,54 @@ +// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +// A simple in-memory cache store. +package cache + +import "time" + +// An item in the key-value cache store. +type item struct { + value string + expires time.Time // Time when the key-value expires +} + +// A key-value cache store. +type Cache struct { + store map[string]item +} + +// Returns a new empty cache store. +func NewCache() *Cache { + c := new(Cache) + c.store = make(map[string]item) + return c +} + +// Set the (key,value) item to the cache store. This item will be +// considered expired after time `expires`. +// +// Cache.Get will return an empty string once `expires` is past the +// current time. +func (c *Cache) Set(key, value string, expires time.Time) { + c.store[key] = item{ + value: value, + expires: expires, + } +} + +// Get an (key,value) item from the cache store by key. +// +// An empty string will be returned when if the key does not exist or +// if the item corresponding to the key has expired. An expired +// (key,value) item will be removed from the cache store. +func (c *Cache) Get(key string) string { + if _, ok := c.store[key]; !ok { + return "" + } + // Check if the item expired. + if time.Until(c.store[key].expires).Seconds() < 0 { + delete(c.store, key) + return "" + } + return c.store[key].value +} |