Skip to content

Commit 21116c3

Browse files
Thomas StrombergThomas Stromberg
authored andcommitted
cleanup
1 parent 6399753 commit 21116c3

13 files changed

+697
-190
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
[![Go Report Card](https://goreportcard.com/badge/github.com/codeGROOVE-dev/bdcache)](https://goreportcard.com/report/github.com/codeGROOVE-dev/bdcache)
55
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
66

7-
Simple, fast, reliable Go cache with [S3-FIFO eviction](https://s3fifo.com/) - better hit rates than LRU.
7+
Simple, fast, secure Go cache with [S3-FIFO eviction](https://s3fifo.com/) - better hit rates than LRU.
88

99
## Why?
1010

1111
- **S3-FIFO Algorithm** - [Superior cache hit rates](https://s3fifo.com/) compared to LRU/LFU
12-
- **Fast** - ~19ns per operation, zero allocations
12+
- **Fast** - ~20ns per operation, zero allocations
13+
- **Secure** - Hardened input validation, no path traversal
1314
- **Reliable** - Memory cache always works, even if persistence fails
14-
- **Smart Persistence** - Local files for dev. Cloud Datastore for Cloud Run
15-
- **Minimal Dependencies** - Only one optional dependency (Cloud Datastore)
15+
- **Smart Persistence** - Local files for dev, Cloud Datastore for Cloud Run
16+
- **Minimal Dependencies** - Only one (Cloud Datastore)
1617

1718
## Install
1819

@@ -48,8 +49,8 @@ cache, err := bdcache.New[string, User](ctx, bdcache.WithBestStore("myapp"))
4849
## Performance
4950

5051
```
51-
BenchmarkCache_Get_Hit-16 66M ops/sec 18.5 ns/op 0 allocs
52-
BenchmarkCache_Set-16 61M ops/sec 19.3 ns/op 0 allocs
52+
BenchmarkCache_Get_Hit-16 63M ops/sec 19.9 ns/op 0 allocs
53+
BenchmarkCache_Set-16 57M ops/sec 20.8 ns/op 0 allocs
5354
```
5455

5556
## License

cache.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package bdcache provides a high-performance cache with S3-FIFO eviction and optional persistence.
12
package bdcache
23

34
import (
@@ -84,7 +85,7 @@ func (c *Cache[K, V]) warmup(ctx context.Context) {
8485

8586
// Get retrieves a value from the cache.
8687
// It first checks the memory cache, then falls back to persistence if available.
87-
func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
88+
func (c *Cache[K, V]) Get(ctx context.Context, key K) (value V, found bool, err error) {
8889
// Check memory first
8990
if val, ok := c.memory.get(key); ok {
9091
return val, true, nil
@@ -96,6 +97,13 @@ func (c *Cache[K, V]) Get(ctx context.Context, key K) (V, bool, error) {
9697
return zero, false, nil
9798
}
9899

100+
// Validate key before accessing persistence (security: prevent path traversal)
101+
if err := c.persist.ValidateKey(key); err != nil {
102+
slog.Warn("invalid key for persistence", "error", err, "key", key)
103+
var zero V
104+
return zero, false, nil
105+
}
106+
99107
// Check persistence
100108
val, expiry, found, err := c.persist.Load(ctx, key)
101109
if err != nil {
@@ -156,6 +164,11 @@ func (c *Cache[K, V]) Delete(ctx context.Context, key K) {
156164

157165
// Remove from persistence if available
158166
if c.persist != nil {
167+
// Validate key before accessing persistence (security: prevent path traversal)
168+
if err := c.persist.ValidateKey(key); err != nil {
169+
slog.Warn("invalid key for persistence delete", "error", err, "key", key)
170+
return
171+
}
159172
if err := c.persist.Delete(ctx, key); err != nil {
160173
// Log error but don't fail - graceful degradation
161174
slog.Warn("persistence delete failed", "error", err, "key", key)

0 commit comments

Comments
 (0)