DiskCache
DiskCache is a Go module that provides a simple and efficient disk-based cache with support for multiple cache instances, each with its own configuration, capacity limits, and persistence across system restarts. The cache items are serialized using MessagePack, compressed, and encrypted with AES-256 before being written to disk.
Features
- Persistence Across Restarts: Cache data is persisted to disk and can be restored upon system restart.
- MessagePack Compression and AES256 Encryption: Data is serialized with MessagePack, compressed, and encrypted with AES256 before being persisted.
- Multiple Cache Instances: Create multiple
DiskCache
instances, each with independent settings and storage directories.
- Per-Instance Configuration: Each cache instance can be configured with different settings.
- Directory Per Instance: Each cache instance stores its files in a unique directory.
- Capacity Limit: Each cache instance cannot exceed 90% of its configured capacity.
Authors
Tech Stack
Installation
To install the DiskCache module, run:
$ go get github.com/greazleay/go-disk-cache
Usage
Here's an example of how to use DiskCache:
package main
import (
"fmt"
"log"
"time"
"github.com/greazleay/go-disk-cache"
)
func main() {
cache, err := diskcache.NewDiskCache()
if err != nil {
log.Fatal(err)
}
// Create a new cache instance
params := diskcache.CreateCacheParams{
CacheName: "example_cache",
MaxSize: 100 * 1024 * 1024, // 100MB max size
EncryptionKey: "your-32-byte-long-encryption-key-here",
Duration: 10 * time.Minute,
}
tokenCache, err := diskcache.NewCacheInstance(¶ms)
if err != nil {
log.Fatal(err)
}
// Set a value in the cache
err = tokenCache.Set("example_key", "example_value")
if err != nil {
log.Fatal(err)
}
// Get a value from the cache
value, err := tokenCache.Get("example_key")
if err != nil {
log.Fatal(err)
}
// decode the value
decodedValue, err := diskcache.DecodeMessagePack[Type](value)
if err != nil {
log.Fatal(err)
}
fmt.Println("Value from cache:", decodedValue)
// Delete a value from the cache
err = cache.Delete("example_key")
if err != nil {
log.Fatal(err)
}
}
Running Tests
To run tests, run the following command
$ go test -v ./...
API
CreateCacheParams
Defines parameters for creating a new disk cache instance.
type CreateCacheParams struct {
CacheName string
MaxSize int64
EncryptionKey string
Duration time.Duration
}
CacheName
: Name of the cache instance.
MaxSize
: Maximum size of the cache in bytes.
EncryptionKey
: Encryption key for AES-256 encryption (must be 32 bytes).
Duration
: Duration for which cache items are valid.
NewDiskCache
Creates a new Disk Cache.
func NewDiskCache() (*diskCache, error)
NewCacheInstance
Creates a new cache instance.
func (cache *diskCache) NewCacheInstance(params *CreateCacheParams) (*cacheInstance, error)
Set
Sets a value in the cache.
func (cache *cacheInstance) Set(key string, value interface{}, duration *time.Duration) error
Get
Gets a value from the cache.
func Get(key string) ([]byte, error)
Delete
Deletes a value from the cache.
func (cache *cacheInstance) Delete(key string) error
CleanUp
Cleans up expired cache items.
func (cache *cacheInstance) CleanUp() error
License
Please see the LICENSE file.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any bugs, improvements, or features.
🔗 Links