A flexible caching solution for conditional HTTP requests (If-None-Match, If-Modified-Since) with support for multiple storage backends.
This library provides a caching mechanism for handling conditional HTTP requests efficiently. It supports three different storage backends:
- Local Memory Cache
- PostgreSQL
- Amazon DynamoDB
The cache helps reduce unnecessary data transfer and server load by properly handling ETags and Last-Modified headers.
- Multiple storage backend options
- Thread-safe operations
- Support for ETags and Last-Modified headers
- Configurable TTL (Time To Live)
- Easy integration with existing applications
- Concurrent access handling
- Automatic cache invalidation
go get github.com/dgduncan/go-cond-cache
import (
"github.com/dgduncan/go-cond-cache"
)
// Initialize a local cache
cache, err := conditionalcache.NewLocalCache(conditionalcache.Config{
TTL: time.Hour * 24,
})
// Initialize a PostgreSQL cache
pgCache, err := conditionalcache.NewPostgresCache(conditionalcache.PostgresConfig{
ConnString: "postgres://user:pass@localhost:5432/dbname",
TableName: "conditional_cache",
})
// Initialize a DynamoDB cache
dynamoCache, err := conditionalcache.NewDynamoCache(conditionalcache.DynamoConfig{
Region: "us-west-2",
TableName: "conditional-cache",
})
// Store a cache entry
err := cache.Set(ctx, "key", CacheEntry{
ETag: "abc123",
LastModified: time.Now(),
Data: []byte("cached data"),
})
// Get a cache entry
entry, err := cache.Get(ctx, "key")
config := conditionalcache.Config{
TTL: time.Hour * 24,
MaxEntries: 1000,
}
config := conditionalcache.PostgresConfig{
ConnString: "postgres://user:pass@localhost:5432/dbname",
TableName: "conditional_cache",
TTL: time.Hour * 24,
CleanupInterval: time.Hour,
}
config := conditionalcache.DynamoConfig{
Region: "us-west-2",
TableName: "conditional-cache",
TTL: time.Hour * 24,
ReadCapacity: 5,
WriteCapacity: 5,
}
CREATE TABLE conditional_cache (
key TEXT PRIMARY KEY,
etag TEXT,
last_modified TIMESTAMP,
data BYTEA,
created_at TIMESTAMP,
expires_at TIMESTAMP
);
{
"TableName": "conditional-cache",
"KeySchema": [
{
"AttributeName": "key",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "key",
"AttributeType": "S"
}
]
}
The library returns detailed errors that can be handled using the provided error types:
switch err.(type) {
case *conditionalcache.NotFoundError:
// Handle cache miss
case *conditionalcache.ExpiredError:
// Handle expired entry
case *conditionalcache.StorageError:
// Handle storage-related errors
}
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by RFC 7232 (Conditional Requests)
- Built with best practices for caching in distributed systems
This README provides a comprehensive overview of your project, including:
- Clear installation instructions
- Usage examples for all three cache types
- Configuration options
- Schema definitions
- Error handling examples
- Contributing guidelines
- License information
You can customize it further based on your specific implementation details, additional features, or requirements.