aboutsummaryrefslogtreecommitdiffstats
path: root/utils.go
diff options
context:
space:
mode:
authorMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-17 18:39:54 +0530
committerMarc Pervaz Boocha <marcpervaz@qburst.com>2025-02-17 18:39:54 +0530
commit6c6535120ca43a57d31b60ae386c34339b44fa10 (patch)
treeb3387a3ed7d096ec0eef60941293397e4201dced /utils.go
parentFirst Commit (diff)
downloadcache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar.gz
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar.bz2
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar.lz
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar.xz
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.tar.zst
cache-6c6535120ca43a57d31b60ae386c34339b44fa10.zip
Bootstraped code for housekeeping operations
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/utils.go b/utils.go
new file mode 100644
index 0000000..9d1408e
--- /dev/null
+++ b/utils.go
@@ -0,0 +1,53 @@
+package cache
+
+import (
+ "hash/fnv"
+ "math"
+ "time"
+)
+
+type pauseTimer struct {
+ *time.Ticker
+ duration time.Duration
+}
+
+func newPauseTimer(d time.Duration) *pauseTimer {
+ ret := &pauseTimer{duration: d}
+ if d != 0 {
+ ret.Ticker = time.NewTicker(d)
+ } else {
+ ret.Ticker = time.NewTicker(math.MaxInt64)
+ ret.Reset(0)
+ }
+ return ret
+}
+
+func (t *pauseTimer) Reset(d time.Duration) {
+ t.duration = d
+ if t.duration == 0 {
+ t.Stop()
+ } else {
+ t.Ticker.Reset(d)
+ }
+}
+
+func (t *pauseTimer) Resume() {
+ t.Reset(t.GetDuration())
+}
+
+func (t *pauseTimer) GetDuration() time.Duration {
+ return t.duration
+}
+
+func zero[T any]() T {
+ var ret T
+ return ret
+}
+
+func hash(data []byte) uint64 {
+ hasher := fnv.New64()
+ if _, err := hasher.Write(data); err != nil {
+ panic(err)
+ }
+ return hasher.Sum64()
+}