Gofmt the codebase.

This commit is contained in:
2025-02-26 00:47:41 +00:00
parent 66c8fef24d
commit 1b7890f322
20 changed files with 349 additions and 348 deletions
+14 -14
View File
@@ -17,8 +17,8 @@ const CompressionThreshold = 1024 // 1KB
const MaxCacheSize = 10000
type CacheEntry struct {
ExpiresAt time.Time
Value []byte
ExpiresAt time.Time
Value []byte
Compressed bool
}
@@ -59,7 +59,7 @@ func (c *Cache) cleanupRoutine(globalTTL time.Duration) {
for range ticker.C {
c.CleanExpiredEntries()
// Trigger GC if we have a lot of entries
if atomic.LoadInt64(&c.entryCount) > MaxCacheSize/2 {
runtime.GC()
@@ -74,7 +74,7 @@ func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
}
expiresAt := time.Now().Add(ttl)
// Only compress if the value is larger than the threshold
var entry CacheEntry
if len(value) > CompressionThreshold {
@@ -100,13 +100,13 @@ func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
Compressed: false,
}
}
// Check if this is a new entry
_, exists := c.entries.Load(key)
if !exists {
atomic.AddInt64(&c.entryCount, 1)
}
c.entries.Store(key, entry)
}
@@ -130,7 +130,7 @@ func (c *Cache) Get(key string) ([]byte, bool) {
}
return value, true
}
return cacheEntry.Value, true
}
@@ -156,7 +156,7 @@ func (c *Cache) compress(data []byte) ([]byte, error) {
var buf bytes.Buffer
w := c.compressPool.Get().(*gzip.Writer)
defer c.compressPool.Put(w)
w.Reset(&buf)
if _, err := w.Write(data); err != nil {
return nil, err
@@ -170,7 +170,7 @@ func (c *Cache) compress(data []byte) ([]byte, error) {
func (c *Cache) decompress(data []byte) ([]byte, error) {
r, ok := c.decompressPool.Get().(*gzip.Reader)
defer c.decompressPool.Put(r)
if !ok || r == nil {
var err error
r, err = gzip.NewReader(bytes.NewReader(data))
@@ -182,7 +182,7 @@ func (c *Cache) decompress(data []byte) ([]byte, error) {
return nil, err
}
}
defer r.Close()
return io.ReadAll(r)
}
@@ -203,10 +203,10 @@ func (c *Cache) CleanExpiredEntries() {
// evictOldest removes the oldest n entries from the cache
func (c *Cache) evictOldest(n int) {
type keyExpiry struct {
key string
key string
expiresAt time.Time
}
// Collect all entries with their expiry times
entries := make([]keyExpiry, 0, n*2)
c.entries.Range(func(k, v interface{}) bool {
@@ -215,7 +215,7 @@ func (c *Cache) evictOldest(n int) {
entries = append(entries, keyExpiry{key, entry.ExpiresAt})
return len(entries) < cap(entries)
})
// Sort by expiry time (oldest first)
// Using a simple selection sort since we only need to find the n oldest
for i := 0; i < n && i < len(entries); i++ {
@@ -229,7 +229,7 @@ func (c *Cache) evictOldest(n int) {
if oldest != i {
entries[i], entries[oldest] = entries[oldest], entries[i]
}
// Delete this entry
if _, exists := c.entries.LoadAndDelete(entries[i].key); exists {
atomic.AddInt64(&c.entryCount, -1)
+19 -19
View File
@@ -14,45 +14,45 @@ const (
func TestMemoryCacheClear(t *testing.T) {
cache := New(DefaultTestExpiration)
// Add some entries
cache.Set("key1", []byte("value1"), DefaultTestExpiration)
cache.Set("key2", []byte("value2"), DefaultTestExpiration)
// Verify entries exist
_, found := cache.Get("key1")
assert.True(t, found, "Expected key1 to exist before clearing cache")
// Clear the cache
cache.Clear()
// Verify cache is empty
_, found = cache.Get("key1")
assert.False(t, found, "Expected key1 to be removed after clearing cache")
_, found = cache.Get("key2")
assert.False(t, found, "Expected key2 to be removed after clearing cache")
// Check that counter was reset
assert.Equal(t, int64(0), cache.CountQueries(), "Expected count to be 0 after clearing cache")
}
func TestMemoryCacheCountQueries(t *testing.T) {
cache := New(DefaultTestExpiration)
// Check initial count
assert.Equal(t, int64(0), cache.CountQueries(), "Expected initial count to be 0")
// Add some entries
cache.Set("key1", []byte("value1"), DefaultTestExpiration)
cache.Set("key2", []byte("value2"), DefaultTestExpiration)
cache.Set("key3", []byte("value3"), DefaultTestExpiration)
// Check count
assert.Equal(t, int64(3), cache.CountQueries(), "Expected count to be 3 after adding 3 entries")
// Delete an entry
cache.Delete("key1")
// Check count after deletion
assert.Equal(t, int64(2), cache.CountQueries(), "Expected count to be 2 after deleting 1 entry")
}
@@ -60,31 +60,31 @@ func TestMemoryCacheCountQueries(t *testing.T) {
func TestMemoryCacheCleanExpiredEntries(t *testing.T) {
// Create a cache with default expiration
cache := New(10 * time.Second)
// Add an entry that will expire quickly
cache.Set("expire-soon", []byte("value1"), 10*time.Millisecond)
// Add an entry that will not expire during the test
cache.Set("expire-later", []byte("value3"), 10*time.Minute)
// Initial count should be 2
assert.Equal(t, int64(2), cache.CountQueries(), "Expected count to be 2 after adding entries")
// Wait for short expiration
time.Sleep(20 * time.Millisecond)
// Get the expired key directly to verify it's expired
_, expiredFound := cache.Get("expire-soon")
assert.False(t, expiredFound, "Key 'expire-soon' should be expired now")
// Verify the not-expired key is still there
val, nonExpiredFound := cache.Get("expire-later")
assert.True(t, nonExpiredFound, "Key 'expire-later' should not be expired")
assert.Equal(t, []byte("value3"), val, "Expected correct value for 'expire-later'")
// Manually clean expired entries
cache.CleanExpiredEntries()
// Count should be 1 now (only the non-expired entry)
assert.Equal(t, int64(1), cache.CountQueries(), "Expected count to be 1 after cleaning expired entries")
}
}