cache
Cache Package
This package provides a flexible and extensible caching interface designed for Go applications. It supports multiple types of caches, allowing you to choose the best caching strategy for your needs. Currently, the package includes an in-memory cache implementation called localcache, with plans to support additional cache types like Redis and Memcached in the future.
Introduction
The Cache Package provides a unified caching interface (Cache[T]) with support for generic types, enabling type-safe caching of any data type. The package is designed with extensibility in mind, allowing for multiple cache implementations that conform to the same interface. This design enables you to switch between different cache backends (e.g., in-memory, Redis, Memcached) without changing your application logic.
Installation
Local Cache
go get github.com/kittipat1413/go-common/framework/cache/localcache
Documentation
For detailed API documentation, examples, and usage patterns, visit the Go Package Documentation.
Usage
Cache Interface
The core of the cache package is the Cache[T] interface, which defines the methods that all cache implementations must provide:
Get: Retrieves a value from the cache. If the key is missing or expired, it uses the providedInitializerfunction to load the value (if theinitializerisniland the key is missing, it returnsErrCacheMiss).Set: Manually sets a value in the cache with a specific expiration duration (if duration isnil, the default expiration is used).Invalidate: Removes a specific key from the cache.InvalidateAll: Clears all items from the cache.
Local Cache Implementation
The localcache package provides an in-memory cache implementation of the Cache[T] interface. It stores items in memory with optional expiration times and supports automatic cleanup of expired items.
Creating a Cache Instance
Customizing Cache Options
You can customize the cache by providing options:
WithDefaultExpiration: Sets the default expiration duration for cache items.WithCleanupInterval: Sets the interval for automatically cleaning up expired items.
Using the Cache
Handling Items Expiration
When adding an item to the cache, you can control its expiration behavior using the Set method:
Custom Duration: You can pass a specific duration for the item to expire.No Expiration: UseNoExpireDurationto make the item persist indefinitely.Default Expiration: If you passnilfor the duration, the cache will use the default expiration set during cache initialization.
Example
You can find a complete working example in the repository under framework/cache/example.
Extensibility
The cache package is designed to be extensible. You can implement additional cache types by creating new packages that conform to the Cache[T] interface.
Implementing a New Cache Type
To implement a new cache type:
Create a New Package: For example,
redis_cache.Implement the
Cache[T]Interface:
Error Handling
The cache package defines a common error for cache misses:
When a key is not found or has expired, the Get method returns ErrCacheMiss. This allows you to handle cache misses distinctly from other errors.
Example:
Last updated