Cache manager with default file and redis driver (rate limiter and verification code manager included).
Cache library contains two different driver by default.
NOTE: You can extend your driver by implementing Cache interface.
for creating file based driver you must pass file name prefix and cache directory to constructor function.
Note: You must call CleanFileExpiration function manually to clear expired records!
import "github.com/gomig/cache"
if fCache := cache.NewFileCache("myApp", "./caches"); fCache != nil {
// Cache driver created
} else {
panic("failed to build cache driver")
}for creating redis based driver you must pass prefix, and redis options to constructor function.
import "github.com/gomig/cache"
if rCache := cache.NewRedisCache("myApp", redis.Options{
Addr: "localhost:6379",
}); rCache != nil {
// Cache driver created
} else {
panic("failed to build cache driver")
}Cache interface contains following methods:
A new value to cache.
// Signature:
Put(key string, value any, ttl time.Duration) error
// Example:
err := rCache.Put("total-debt", 410203, 100 * time.Hour)Put value with infinite ttl.
// Signature:
PutForever(key string, value any) error
// Example:
err := rCache.PutForever("base-discount", 10)Change value of cache item and return false if item not exists (this. methods keep cache ttl).
Cation: set value on non exists item will generate error. please check if item exists before set or use put method instead!
// Signature:
Set(key string, value any) (bool, error)
// Example:
ok, err := rCache.Set("base-discount", 15)Get item from cache.
// Signature:
Get(key string) (any, error)
// Example:
v, err := rCache.Get("total-users")Check if item exists in cache.
// Signature:
Exists(key string) (bool, error)
// Example:
exists, err := rCache.Exists("total-users");Delete Item from cache.
// Signature:
Forget(key string) error
// Example:
err := rCache.Forget("total-users")Item from cache and then remove it.
// Signature:
Pull(key string) (any, error)
// Example:
v, err := rCache.Pull("total-users")Get cache item ttl. This method returns -1 if item not exists.
// Signature:
TTL(key string) (time.Duration, error)
// Example:
ttl, err := rCache.TTL("total-users")Parse cache item as caster.
// Signature:
Cast(key string) (caster.Caster, error)
// Example:
c, err := rCache.Cast("total-users")
v, err := c.Int32()Increment numeric item by float, return false if item not exists
// Signature:
IncrementFloat(key string, value float64) (bool, error)
// Example:
err := rCache.IncrementFloat("some-float", 0.01)Increment numeric item by int, return false if item not exists
// Signature:
Increment(key string, value int64) (bool, error)
// Example:
err := rCache.Increment("some-number", 10)Decrement numeric item by float, return false if item not exists
// Signature:
DecrementFloat(key string, value float64) (bool, error)
// Example:
err := rCache.DecrementFloat("some-float", 0.29)Decrement numeric item by int, return false if item not exists
// Signature:
Decrement(key string, value int64) (bool, error)
// Example:
err := rCache.Decrement("total-try", 1)func NewRedisQueue(name string, opt redis.Options) QueueQueue new item.
Read first queue item.
Note: Rate limiter based on cache, For creating rate limiter driver you must pass a cache driver instance to constructor function.
// Signature:
NewRateLimiter(key string, maxAttempts uint32, ttl time.Duration, cache Cache) (RateLimiter, error)
// Example: allow 3 attempts every 60 seconds
import "github.com/gomig/cache"
limiter, err := cache.NewRateLimiter("login-attempts", 3, 60 * time.Second, rCache)Rate limiter interface contains following methods:
Decrease the allowed times.
// Signature:
Hit() error
// Example:
err := limiter.Hit()Lock rate limiter.
// Signature:
Lock() error
// Example:
err := limiter.Lock() // no more attempts leftReset rate limiter (clear total attempts).
// Signature:
Reset() error
// Example:
err := limiter.Reset()Remove rate limiter record. call any method after clear with generate "NotExists" error!
// Signature:
Clear() error
// Example:
err := limiter.Clear()Check if rate limiter must lock access.
// Signature:
MustLock() (bool, error)
// Example:
if locked, _:= limiter.MustLock(), locked {
// Block access
}Get user attempts count.
// Signature:
TotalAttempts() (uint32, error)
// Example:
totalAtt, err := limiter.TotalAttempts() // 3Get user retries left.
// Signature:
RetriesLeft() (uint32, error)
// Example:
leftRet, err := limiter.RetriesLeft() // 2Get time until unlock.
// Signature:
AvailableIn() (time.Duration, error)
// Example:
availableIn, err := limiter.AvailableIn()verification code used for managing verification code sent to user.
Note: Verification code based on cache, For creating verification code driver you must pass a cache driver instance to constructor function.
// Signature:
NewVerificationCode(key string, ttl time.Duration, cache Cache) (VerificationCode, error)
// Example:
import "github.com/gomig/cache"
vCode, err := cache.NewVerificationCode("phone-verification", 5 * time.Minute, rCache)Verification code interface contains following methods:
Set code. You can set code directly or use generator methods.
// Signature:
Set(value string) error
// Example:
err := vCode.Set("ABD531")Generate a random numeric code with 5 character length and set as code.
// Signature:
Generate() (string, error)
// Example:
code, err := vCode.Generate()Generate a random numeric code with special character length and set as code.
// Signature:
GenerateN(count uint) (string, error)
// Example:
code, err := vCode.GenerateN(6)Clear code from cache.
// Signature:
Clear() error
// Example:
err := vCode.Clear()Get code.
// Signature:
Get() (string, error)
// Example:
code, err := vCode.Get()Exists check if code exists in cache and not empty.
// Signature:
Exists() (bool, error)
// Example:
exists, err := vCode.Exists()Get token ttl.
// Signature:
TTL() (time.Duration, error)
// Example:
ttl, err := vCode.TTl()