This library provides a flexible and extensible caching system with support for in-memory and Redis-based caching. It includes utilities for managing rate limiters, queues, and verification codes.
The Cache
interface provides a unified API for caching operations:
Put(key string, value any, ttl *time.Duration) error
: Store a value with an optional TTL.Update(key string, value any) (bool, error)
: Update an existing key.PutOrUpdate(key string, value any, ttl *time.Duration) error
: Store or update a value.Get(key string) (any, error)
: Retrieve a value.Pull(key string) (any, error)
: Retrieve and remove a value.Cast(key string) (cast.Caster, error)
: Retrieve and cast a value.Exists(key string) (bool, error)
: Check if a key exists.Forget(key string) error
: Remove a key.TTL(key string) (time.Duration, error)
: Get the TTL of a key.Increment(key string, value int64) (bool, error)
: Increment a numeric value.Decrement(key string, value int64) (bool, error)
: Decrement a numeric value.IncrementFloat(key string, value float64) (bool, error)
: Increment a float value.DecrementFloat(key string, value float64) (bool, error)
: Decrement a float value.
The MemoryCache
is an in-memory implementation of the Cache
interface:
cache := cache.NewMemoryCache()
ttl := 5 * time.Second
err := cache.Put("key", "value", &ttl)
value, err := cache.Get("key")
The RedisCache
is a Redis-based implementation of the Cache
interface:
redisClient := redis.NewClient(&redis.Options{})
cache := cache.NewRedisCache("prefix", redisClient)
ttl := 5 * time.Second
err := cache.Put("key", "value", &ttl)
value, err := cache.Get("key")
The Queue
provides methods for managing a queue:
Push(value any) error
: Add a value.Pull() (any, error)
: Retrieve and remove the first item.Pop() (any, error)
: Retrieve and remove the last item.Cast() (cast.Caster, error)
: Retrieve and cast the first item.Length() (int64, error)
: Get the number of items.Clear() error
: Remove all items.
The RateLimiter
manages rate limits:
Hit() error
: Decrement remaining attempts.Lock() error
: Lock the rate limiter.Reset() error
: Reset the rate limiter.Clear() error
: Remove the rate limiter.MustLock() (bool, error)
: Check if locking is required.TotalAttempts() (uint32, error)
: Get total attempts.RetriesLeft() (uint32, error)
: Get remaining attempts.AvailableIn() (time.Duration, error)
: Time until unlock.
The VerificationCode
manages verification codes:
Set(code string) error
: Store a code.Generate(count uint) (string, error)
: Generate a random code.Clear() error
: Clear the code.Get() (string, error)
: Retrieve the code.Validate(code string) (bool, error)
: Validate a code.Exists() (bool, error)
: Check if a code exists.TTL() (time.Duration, error)
: Get the TTL of the code.
This project is licensed under the ISC License. See the LICENSE file for details.