-
Notifications
You must be signed in to change notification settings - Fork 0
LevelDB Cache #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||||||||
| package leveldb | ||||||||||||
|
||||||||||||
|
|
||||||||||||
| import ( | ||||||||||||
| "errors" | ||||||||||||
| "log/slog" | ||||||||||||
|
|
||||||||||||
| "github.com/syndtr/goleveldb/leveldb" | ||||||||||||
| "go.rtnl.ai/httpcache" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| // Cache is an implementation of httpcache.Cache with leveldb storage | ||||||||||||
| type Cache struct { | ||||||||||||
| db *leveldb.DB | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+11
to
+14
|
||||||||||||
|
|
||||||||||||
| // New returns a cache that will store cached data in a leveldb database at the path. | ||||||||||||
| func New(path string) (_ *Cache, err error) { | ||||||||||||
| cache := &Cache{} | ||||||||||||
| if cache.db, err = leveldb.OpenFile(path, nil); err != nil { | ||||||||||||
| return nil, err | ||||||||||||
| } | ||||||||||||
| return cache, nil | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Make returns a cache using the specified db instance as the underlying storage. | ||||||||||||
| func Make(db *leveldb.DB) *Cache { | ||||||||||||
|
||||||||||||
| func Make(db *leveldb.DB) *Cache { | |
| func Make(db *leveldb.DB) *Cache { | |
| if db == nil { | |
| panic("leveldb: nil *leveldb.DB passed to leveldb.Make") | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package leveldb_test | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "go.rtnl.ai/httpcache/leveldb" | ||
| ) | ||
|
|
||
| func benchmarkGet(size int) func(b *testing.B) { | ||
| return func(b *testing.B) { | ||
| path := filepath.Join(b.TempDir(), "cache.db") | ||
| cache, err := leveldb.New(path) | ||
| require.NoError(b, err) | ||
| defer cache.Close() | ||
|
Comment on lines
+11
to
+16
|
||
|
|
||
| value := make([]byte, size) | ||
|
|
||
| // Prepopulate the cache | ||
| for i := 0; i < 128; i++ { | ||
| key := string(rune('a' + i)) | ||
| cache.Put(key, value) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| cache.Get(string(rune('a' + i%192))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkLevelDBCacheGet(b *testing.B) { | ||
| b.Run("Small", benchmarkGet(512)) | ||
| b.Run("Realistic", benchmarkGet(2048)) | ||
| b.Run("Large", benchmarkGet(5.243e+6)) | ||
| } | ||
|
|
||
| func benchmarkPut(size int) func(b *testing.B) { | ||
| return func(b *testing.B) { | ||
| path := filepath.Join(b.TempDir(), "cache.db") | ||
| cache, err := leveldb.New(path) | ||
| require.NoError(b, err) | ||
| defer cache.Close() | ||
|
Comment on lines
+39
to
+44
|
||
|
|
||
| value := make([]byte, size) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| cache.Put(string(rune('a'+i%192)), value) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkLevelDBCachePut(b *testing.B) { | ||
| b.Run("Small", benchmarkPut(512)) | ||
| b.Run("Realistic", benchmarkPut(2048)) | ||
| b.Run("Large", benchmarkPut(5.243e+6)) | ||
| } | ||
|
|
||
| // Benchmark mixed operations | ||
| func BenchmarkLevelDBCacheMixed(b *testing.B) { | ||
| path := filepath.Join(b.TempDir(), "cache.db") | ||
| cache, err := leveldb.New(path) | ||
| require.NoError(b, err) | ||
| defer cache.Close() | ||
|
Comment on lines
+62
to
+66
|
||
|
|
||
| value := make([]byte, 1024) | ||
|
|
||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| key := string(rune('a' + i%128)) | ||
| switch i % 3 { | ||
| case 0: | ||
| cache.Put(key, value) | ||
| case 1: | ||
| cache.Get(key) | ||
| case 2: | ||
| cache.Del(key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark concurrent mixed operations | ||
| func BenchmarkLevelDBCacheParallelMixed(b *testing.B) { | ||
| path := filepath.Join(b.TempDir(), "cache.db") | ||
| cache, err := leveldb.New(path) | ||
| require.NoError(b, err) | ||
| defer cache.Close() | ||
|
Comment on lines
+85
to
+89
|
||
|
|
||
| value := make([]byte, 1024) | ||
|
|
||
| b.RunParallel(func(pb *testing.PB) { | ||
| i := 0 | ||
| for pb.Next() { | ||
| key := string(rune('a' + i%128)) | ||
| switch i % 3 { | ||
| case 0: | ||
| cache.Put(key, value) | ||
| case 1: | ||
| cache.Get(key) | ||
| case 2: | ||
| cache.Del(key) | ||
| } | ||
| i++ | ||
| } | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package leveldb_test | ||
|
|
||
| import ( | ||
| "math/rand/v2" | ||
| "path/filepath" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "go.rtnl.ai/httpcache/leveldb" | ||
| ) | ||
|
|
||
| func TestLevelDBCache(t *testing.T) { | ||
| path := filepath.Join(t.TempDir(), "cache.db") | ||
|
|
||
| cache, err := leveldb.New(path) | ||
| require.NoError(t, err) | ||
| defer cache.Close() | ||
|
|
||
| cache.Put("foo", []byte("bar")) | ||
|
|
||
| val, ok := cache.Get("foo") | ||
| require.True(t, ok) | ||
| require.Equal(t, []byte("bar"), val) | ||
|
|
||
| cache.Del("foo") | ||
| _, ok = cache.Get("foo") | ||
| require.False(t, ok) | ||
| } | ||
|
|
||
| func TestLevelDBRace(t *testing.T) { | ||
| // Ensures no race conditions occur during concurrent access. | ||
| path := filepath.Join(t.TempDir(), "cache.db") | ||
| cache, err := leveldb.New(path) | ||
| require.NoError(t, err) | ||
| defer cache.Close() | ||
|
|
||
| value := make([]byte, 2048) | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < 16; i++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for j := 0; j < 512; j++ { | ||
| k := rand.IntN(64) | ||
| key := string(rune('a' + k%16)) | ||
| switch k % 3 { | ||
| case 0: | ||
| cache.Put(key, value) | ||
| case 1: | ||
| cache.Get(key) | ||
| case 2: | ||
| cache.Del(key) | ||
| } | ||
| } | ||
| }() | ||
| } | ||
| wg.Wait() | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version of golang/snappy is from 2018 (commit 2e65f85255db). Consider updating to a more recent version for potential bug fixes and performance improvements. The goleveldb dependency may be pulling in an outdated transitive dependency.