https://github.com/go-universal/cache
Advanced Redis and in-memory caching for Go.
https://github.com/go-universal/cache
caching golang queue rate-limiting redis-cache verification-code
Last synced: 10 months ago
JSON representation
Advanced Redis and in-memory caching for Go.
- Host: GitHub
- URL: https://github.com/go-universal/cache
- Owner: go-universal
- License: isc
- Created: 2025-04-08T06:36:16.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2025-04-08T09:22:24.000Z (10 months ago)
- Last Synced: 2025-04-08T10:22:19.994Z (10 months ago)
- Topics: caching, golang, queue, rate-limiting, redis-cache, verification-code
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cache Library Documentation

[](https://pkg.go.dev/github.com/go-universal/cache)
[](https://github.com/go-universal/cache/blob/main/LICENSE)
[](https://goreportcard.com/report/github.com/go-universal/cache)


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.
## Cache
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.
## Memory Cache
The `MemoryCache` is an in-memory implementation of the `Cache` interface:
```go
cache := cache.NewMemoryCache()
ttl := 5 * time.Second
err := cache.Put("key", "value", &ttl)
value, err := cache.Get("key")
```
## Redis Cache
The `RedisCache` is a Redis-based implementation of the `Cache` interface:
```go
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")
```
## Queue
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.
## Rate Limiter
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.
## Verification Code
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.
## License
This project is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.