// // icache.go // Copyright (C) 2023 tiglog // // Distributed under terms of the MIT license. // package gcache import "time" // ExpiredCallback Callback the function when the key-value pair expires // Note that it is executed after expiration type ExpiredCallback func(k string, v interface{}) error type ICache interface { //Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. //Any previous time to live associated with the key is discarded on successful SET operation. //Example: //c.Set("demo", 1) //c.Set("demo", 1, WithEx(10*time.Second)) //c.Set("demo", 1, WithEx(10*time.Second), WithNx()) Set(k string, v interface{}, opts ...SetIOption) bool //Get the value of key. //If the key does not exist the special value nil,false is returned. //Example: //c.Get("demo") //nil, false //c.Set("demo", "value") //c.Get("demo") //"value", true Get(k string) (interface{}, bool) //GetSet Atomically sets key to value and returns the old value stored at key. //Returns nil,false when key not exists. //Example: //c.GetSet("demo", 1) //nil,false //c.GetSet("demo", 2) //1,true GetSet(k string, v interface{}, opts ...SetIOption) (interface{}, bool) //GetDel Get the value of key and delete the key. //This command is similar to GET, except for the fact that it also deletes the key on success. //Example: //c.Set("demo", "value") //c.GetDel("demo") //"value", true //c.GetDel("demo") //nil, false GetDel(k string) (interface{}, bool) //Del Removes the specified keys. A key is ignored if it does not exist. //Return the number of keys that were removed. //Example: //c.Set("demo1", "1") //c.Set("demo2", "1") //c.Del("demo1", "demo2", "demo3") //2 Del(keys ...string) int //DelExpired Only delete when key expires //Example: //c.Set("demo1", "1") //c.Set("demo2", "1", WithEx(1*time.Second)) //time.Sleep(1*time.Second) //c.DelExpired("demo1", "demo2") //true DelExpired(k string) bool //Exists Returns if key exists. //Return the number of exists keys. //Example: //c.Set("demo1", "1") //c.Set("demo2", "1") //c.Exists("demo1", "demo2", "demo3") //2 Exists(keys ...string) bool //Expire Set a timeout on key. //After the timeout has expired, the key will automatically be deleted. //Return false if the key not exist. //Example: //c.Expire("demo", 1*time.Second) // false //c.Set("demo", "1") //c.Expire("demo", 1*time.Second) // true Expire(k string, d time.Duration) bool //ExpireAt has the same effect and semantic as Expire, but instead of specifying the number of seconds representing the TTL (time to live), //it takes an absolute Unix Time (seconds since January 1, 1970). A Time in the past will delete the key immediately. //Return false if the key not exist. //Example: //c.ExpireAt("demo", time.Now().Add(10*time.Second)) // false //c.Set("demo", "1") //c.ExpireAt("demo", time.Now().Add(10*time.Second)) // true ExpireAt(k string, t time.Time) bool //Persist Remove the existing timeout on key. //Return false if the key not exist. //Example: //c.Persist("demo") // false //c.Set("demo", "1") //c.Persist("demo") // true Persist(k string) bool //Ttl Returns the remaining time to live of a key that has a timeout. //Returns 0,false if the key does not exist or if the key exist but has no associated expire. //Example: //c.Set("demo", "1") //c.Ttl("demo") // 0,false //c.Set("demo", "1", WithEx(10*time.Second)) //c.Ttl("demo") // 10*time.Second,true Ttl(k string) (time.Duration, bool) } type IItem interface { Expired() bool CanExpire() bool SetExpireAt(t time.Time) } type Item struct { v interface{} expire time.Time } func (i *Item) Expired() bool { if !i.CanExpire() { return false } return time.Now().After(i.expire) } func (i *Item) CanExpire() bool { return !i.expire.IsZero() } func (i *Item) SetExpireAt(t time.Time) { i.expire = t }