Documentation
¶
Index ¶
- type Cacher
- func (c *Cacher[C, T]) Delete(key C)
- func (c *Cacher[C, T]) DeleteSome(cond SegrigatorFunc[T])
- func (c *Cacher[C, T]) Get(key C) (value T, ok bool)
- func (c *Cacher[C, T]) GetAll() []T
- func (c *Cacher[C, T]) GetSome(cond SegrigatorFunc[T]) []T
- func (c *Cacher[C, T]) NumKeys() int
- func (c *Cacher[C, T]) Reset()
- func (c *Cacher[C, T]) Set(key C, val T)
- func (c *Cacher[C, T]) SetWithTTL(key C, val T, ttl time.Duration)
- type CleaningMode
- type Duplet
- type NewCacherOpts
- type PolyKeyer
- type SegrigatorFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cacher ¶
type Cacher[C comparable, T any] struct { // contains filtered or unexported fields }
Cacher is a fast, decentralised caching system, generic in nature and uses Go's in built mapping to store data. It has a plenty of features like TTL, Revaluation etc.
Some of the main features are descried below:
TTL (Time-To-Live): It allows us to expire a key after a specific time period. Eg: If TTL is set to 30 seconds, then each key of current Cacher will be expired after 30 seconds of their addition.
Revaluation: This is another useful feature that allows us to keep keys cached as per their usage frequency. Working: Whenever the keys will be retrieved via (Cacher.Get) method, its expiry will be renewed and this will allow us to keep frequently used keys in the map without expiration.
func NewCacher ¶
func NewCacher[KeyT comparable, ValueT any](opts *NewCacherOpts) *Cacher[KeyT, ValueT]
NewCacher is a generic function which creates a new Cacher instance.
Generic parameters (for current Cacher instance):
KeyT: It is the "static" type of keys of our cache. It accepts types which implement built-in comparable interface. Eg: If it is set to string, then keys will only be allowed as a string built-in data-type.
ValueT: It is the type of values of our cache. It can be set to any type. Eg: If it is set to string, then value will only be allowed as a string built-in data-type.
Input parameters:
opts (type *NewCacherOpts): It contains optional parameters which you can use while creating a new Cacher instance.
General Example: c := cacher.NewCacher[int, string](&cacher.NewCacherOpts{10*time.Minute, time.Hour, true}) will create a new Cacher instance which will expire keys after 10 minutes of their addition to the system, all the expired keys will be deleted from cache once in an hour. Keys will have their expiry revalueted on every c.Get call.
func (*Cacher[C, T]) Delete ¶
func (c *Cacher[C, T]) Delete(key C)
Delete is used to delete the input key from current Cacher instance. It doesn't return anything. If there is no such key, Delete is a no-op.
func (*Cacher[C, T]) DeleteSome ¶
func (c *Cacher[C, T]) DeleteSome(cond SegrigatorFunc[T])
DeleteSome is used to delete keys which satisfied a particular condition determined via SegrigatorFunc.
func (*Cacher[C, T]) Get ¶
Set is used to get value of the input key. It returns value of input key with true while returns empty value with false if key is not found or has expired already
Note: It will renew the expiration time of the input key which is retrieved if revaluation mode is on for current Cacher instance.
func (*Cacher[C, T]) GetAll ¶
func (c *Cacher[C, T]) GetAll() []T
GetAll is used to return all the unexpired key-value pairs present in the current Cacher instance, returns a slice of values.
Note: It doesn't renew expiration time of any key even if the revaluation mode is turned on for the current Cacher instance.
func (*Cacher[C, T]) GetSome ¶
func (c *Cacher[C, T]) GetSome(cond SegrigatorFunc[T]) []T
GetSome is used to get keys which satisfired a particular condition determined via SegrigatorFunc. It returns those values which satisfied the condition determined via SegrigatorFunc.
func (*Cacher[C, T]) NumKeys ¶
NumKeys counts the number of keys present in the current Cacher instance and returns that count.
func (*Cacher[C, T]) Reset ¶
func (c *Cacher[C, T]) Reset()
The Reset function deletes the current cache map and reallocates an empty one in place of it. Use it if you want to delete all keys at once. It doesn't return anything.
func (*Cacher[C, T]) Set ¶
func (c *Cacher[C, T]) Set(key C, val T)
Set is used to set a new key-value pair to the current Cacher instance. It doesn't return anything.
func (*Cacher[C, T]) SetWithTTL ¶ added in v1.0.2
SetWithTTL is used to set a new key-value pair to the current Cacher instance with a specific TTL. It doesn't return anything. It will expire the key after the input TTL, and TTL specified in this function will override the default TTL of current Cacher instance for this pair specifically.
type CleaningMode ¶ added in v1.0.3
type CleaningMode int
const ( CleaningNone CleaningMode = iota CleaningCentral CleaningLocal )
type Duplet ¶
type Duplet struct {
// contains filtered or unexported fields
}
Dupley is a special type of PolyKeyer which is used when you want to set exactly 2 parameters as a key to some value. It creates a single key of type string from 2 values, and looks like: "primaryKey.secondaryKey" where dot (.) works as a key separator, primaryKey is the 1st key and set while making a new Duplet, while secondaryKey is provided while creating keys from a duplet.
Note: You can use PolyKeyer in case you want to use more than one extra parameter.
Example ¶
// 2 is number of extra keys we'd use.
// total keys become 3 then
keyer := NewDuplet("chat")
fmt.Println(keyer.New(fmt.Sprint(100291)))
fmt.Println(keyer.New(fmt.Sprint(100292)))
Output: chat.100291 chat.100292
func (*Duplet) New ¶
It creates a new unity key of type string with 1st key as the primary key of current Duplet and secondary key as the one passed which was passed as an argument to this function.
Example ¶
// 2 is number of extra keys we'd use.
// total keys become 3 then
keyer := NewPolyKeyer("chat", 2)
fmt.Println(keyer.New("public", fmt.Sprint(100291)))
fmt.Println(keyer.New("private", fmt.Sprint(100292)))
Output: chat.public.100291 chat.private.100292
type NewCacherOpts ¶
type NewCacherOpts struct {
TimeToLive time.Duration
CleanInterval time.Duration
Revaluate bool
CleanerMode CleaningMode
}
This struct contains the optional arguments which can be filled while creating a new Cacher instance.
Parameters:
TimeToLive (type time.Duration): It allows us to expire a key after a specific time period. Eg: If it is set to 30 seconds, then each key of current Cacher will be expired after 30 seconds of their addition.
CleanInterval (type time.Duration): It is the time of interval between two cleaner windows. A cleaner window is that time frame when all the expired keys will be deleted from our cache mapping. Note: It TTL is set to a finite value and no value is passed to CleanInterval, it'll use a default time interval of 1 day for clean window. Eg: If CleanInterval is set to 1 hour, then cleaner window will be run after every 1 hour, and the expired keys which are present in our cache map will be deleted.
Revaluate (type bool): It allows us to keep keys cached as per their usage frequency. Working: Whenever the keys will be retrieved via (Cacher.Get) method, its expiry will be renewed and this will allow us to keep frequently used keys in the map without expiration.
type PolyKeyer ¶
type PolyKeyer struct {
// contains filtered or unexported fields
}
PolyKeyer is a special type of struct which is used when you want to set more than 1 parameter as a key to some value. It creates a single key of type string from multiple values, and looks like: "primaryKey.extraKey1.extraKey2.extraKeyn" where dot (.) works as a key separator, primaryKey is the 1st key and set while making a new PolyKeyer, while extraKeys are provided while creating keys from a polykeyer.
Note: You should use Duplet in case there is only one extra parameter.
Example ¶
// 2 is number of extra keys we'd use.
// total keys become 3 then
keyer := NewPolyKeyer("chat", 2)
fmt.Println(keyer.New("public", fmt.Sprint(100291)))
fmt.Println(keyer.New("private", fmt.Sprint(100292)))
Output: chat.public.100291 chat.private.100292
func NewPolyKeyer ¶
This function creates a new PolyKeyer instance with the provided primary key and number of extra keys. Eg: If we pass "chat" to primaryKey and 2 to numExtraKeys then this function will create a new PolyKeyer which would create unified key of type string with 3 keys in it.
func (*PolyKeyer) New ¶
It creates a new unity key of type string with 1st key as the primary key of current PolyKeyer and rest of the keys in the same order as they were passed as an argument.
Example ¶
// 2 is number of extra keys we'd use.
// total keys become 3 then
keyer := NewPolyKeyer("chat", 2)
fmt.Println(keyer.New("public", fmt.Sprint(100291)))
fmt.Println(keyer.New("private", fmt.Sprint(100292)))
Output: chat.public.100291 chat.private.100292
type SegrigatorFunc ¶
SegrigatorFunc takes the input as value of current key. Returned boolean is used for segrigation of keys for GetSome function.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
multiKey
module
|
|
|
singleKey/builtin-builtin
module
|
|
|
singleKey/builtin-custom
module
|
|
|
singleKey/custom-custom
module
|
Above screenshot contains benchmark of the situation when you'll need to serialise your struct to bytes to store in BigCache/FreeCache, while you won't need to do that in case of Cacher since it uses Generics.
We used "encoding/gob" for serialising the value struct to bytes
Above screenshot contains benchmark of the situation when you won't need to serialise your value to bytes or we can say, serialisation time removed.