Skip to content

Commit 64cbdd6

Browse files
committed
[lint] fix more issues
1 parent 8a517fa commit 64cbdd6

File tree

10 files changed

+119
-113
lines changed

10 files changed

+119
-113
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ linters:
476476
- errcheck
477477
- exhaustruct
478478
- funlen
479+
- gocognit
479480
- goconst
480481
- gosec
481482
- noctx

pkg/cache/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cache
33
import "errors"
44

55
var (
6+
// ErrInvalidConfig indicates an invalid configuration.
7+
ErrInvalidConfig = errors.New("invalid config")
68
// ErrKeyNotFound indicates no value exists for the given key.
79
ErrKeyNotFound = errors.New("key not found")
810
// ErrKeyExpired indicates a value exists but has expired.

pkg/cache/memory.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"time"
77
)
88

9-
type memoryCache struct {
9+
type MemoryCache struct {
1010
items map[string]*memoryItem
1111
ttl time.Duration
1212

1313
mux sync.RWMutex
1414
}
1515

16-
func NewMemory(ttl time.Duration) *memoryCache {
17-
return &memoryCache{
16+
func NewMemory(ttl time.Duration) *MemoryCache {
17+
return &MemoryCache{
1818
items: make(map[string]*memoryItem),
1919
ttl: ttl,
2020

@@ -45,14 +45,14 @@ func (i *memoryItem) isExpired(now time.Time) bool {
4545
}
4646

4747
// Cleanup implements Cache.
48-
func (m *memoryCache) Cleanup(_ context.Context) error {
48+
func (m *MemoryCache) Cleanup(_ context.Context) error {
4949
m.cleanup(func() {})
5050

5151
return nil
5252
}
5353

5454
// Delete implements Cache.
55-
func (m *memoryCache) Delete(_ context.Context, key string) error {
55+
func (m *MemoryCache) Delete(_ context.Context, key string) error {
5656
m.mux.Lock()
5757
delete(m.items, key)
5858
m.mux.Unlock()
@@ -61,7 +61,7 @@ func (m *memoryCache) Delete(_ context.Context, key string) error {
6161
}
6262

6363
// Drain implements Cache.
64-
func (m *memoryCache) Drain(_ context.Context) (map[string][]byte, error) {
64+
func (m *MemoryCache) Drain(_ context.Context) (map[string][]byte, error) {
6565
var cpy map[string]*memoryItem
6666

6767
m.cleanup(func() {
@@ -78,7 +78,7 @@ func (m *memoryCache) Drain(_ context.Context) (map[string][]byte, error) {
7878
}
7979

8080
// Get implements Cache.
81-
func (m *memoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]byte, error) {
81+
func (m *MemoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]byte, error) {
8282
return m.getValue(func() (*memoryItem, bool) {
8383
if len(opts) == 0 {
8484
m.mux.RLock()
@@ -88,23 +88,25 @@ func (m *memoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]b
8888
return item, ok
8989
}
9090

91-
o := getOptions{}
91+
o := new(getOptions)
9292
o.apply(opts...)
9393

9494
m.mux.Lock()
9595
item, ok := m.items[key]
96-
if !ok {
96+
97+
if !ok { //nolint:revive,gocritic //simplify code structure
9798
// item not found, nothing to do
9899
} else if o.delete {
99100
delete(m.items, key)
100101
} else if !item.isExpired(time.Now()) {
101-
if o.validUntil != nil {
102+
switch {
103+
case o.validUntil != nil:
102104
item.validUntil = *o.validUntil
103-
} else if o.setTTL != nil {
105+
case o.setTTL != nil:
104106
item.validUntil = time.Now().Add(*o.setTTL)
105-
} else if o.updateTTL != nil {
107+
case o.updateTTL != nil:
106108
item.validUntil = item.validUntil.Add(*o.updateTTL)
107-
} else if o.defaultTTL {
109+
case o.defaultTTL:
108110
item.validUntil = time.Now().Add(m.ttl)
109111
}
110112
}
@@ -115,12 +117,12 @@ func (m *memoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]b
115117
}
116118

117119
// GetAndDelete implements Cache.
118-
func (m *memoryCache) GetAndDelete(ctx context.Context, key string) ([]byte, error) {
120+
func (m *MemoryCache) GetAndDelete(ctx context.Context, key string) ([]byte, error) {
119121
return m.Get(ctx, key, AndDelete())
120122
}
121123

122124
// Set implements Cache.
123-
func (m *memoryCache) Set(_ context.Context, key string, value []byte, opts ...Option) error {
125+
func (m *MemoryCache) Set(_ context.Context, key string, value []byte, opts ...Option) error {
124126
m.mux.Lock()
125127
m.items[key] = m.newItem(value, opts...)
126128
m.mux.Unlock()
@@ -129,7 +131,7 @@ func (m *memoryCache) Set(_ context.Context, key string, value []byte, opts ...O
129131
}
130132

131133
// SetOrFail implements Cache.
132-
func (m *memoryCache) SetOrFail(_ context.Context, key string, value []byte, opts ...Option) error {
134+
func (m *MemoryCache) SetOrFail(_ context.Context, key string, value []byte, opts ...Option) error {
133135
m.mux.Lock()
134136
defer m.mux.Unlock()
135137

@@ -143,7 +145,7 @@ func (m *memoryCache) SetOrFail(_ context.Context, key string, value []byte, opt
143145
return nil
144146
}
145147

146-
func (m *memoryCache) newItem(value []byte, opts ...Option) *memoryItem {
148+
func (m *MemoryCache) newItem(value []byte, opts ...Option) *memoryItem {
147149
o := options{
148150
validUntil: time.Time{},
149151
}
@@ -155,7 +157,7 @@ func (m *memoryCache) newItem(value []byte, opts ...Option) *memoryItem {
155157
return newItem(value, o)
156158
}
157159

158-
func (m *memoryCache) getItem(getter func() (*memoryItem, bool)) (*memoryItem, error) {
160+
func (m *MemoryCache) getItem(getter func() (*memoryItem, bool)) (*memoryItem, error) {
159161
item, ok := getter()
160162

161163
if !ok {
@@ -169,7 +171,7 @@ func (m *memoryCache) getItem(getter func() (*memoryItem, bool)) (*memoryItem, e
169171
return item, nil
170172
}
171173

172-
func (m *memoryCache) getValue(getter func() (*memoryItem, bool)) ([]byte, error) {
174+
func (m *MemoryCache) getValue(getter func() (*memoryItem, bool)) ([]byte, error) {
173175
item, err := m.getItem(getter)
174176
if err != nil {
175177
return nil, err
@@ -178,7 +180,7 @@ func (m *memoryCache) getValue(getter func() (*memoryItem, bool)) ([]byte, error
178180
return item.value, nil
179181
}
180182

181-
func (m *memoryCache) cleanup(cb func()) {
183+
func (m *MemoryCache) cleanup(cb func()) {
182184
t := time.Now()
183185

184186
m.mux.Lock()
@@ -192,8 +194,8 @@ func (m *memoryCache) cleanup(cb func()) {
192194
m.mux.Unlock()
193195
}
194196

195-
func (m *memoryCache) Close() error {
197+
func (m *MemoryCache) Close() error {
196198
return nil
197199
}
198200

199-
var _ Cache = (*memoryCache)(nil)
201+
var _ Cache = (*MemoryCache)(nil)

pkg/cache/memory_bench_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//nolint:errcheck
21
package cache_test
32

43
import (
@@ -12,7 +11,7 @@ import (
1211
"github.com/android-sms-gateway/server/pkg/cache"
1312
)
1413

15-
// BenchmarkMemoryCache_Set measures the performance of Set operations
14+
// BenchmarkMemoryCache_Set measures the performance of Set operations.
1615
func BenchmarkMemoryCache_Set(b *testing.B) {
1716
cache := cache.NewMemory(0)
1817
ctx := context.Background()
@@ -27,7 +26,7 @@ func BenchmarkMemoryCache_Set(b *testing.B) {
2726
})
2827
}
2928

30-
// BenchmarkMemoryCache_Get measures the performance of Get operations
29+
// BenchmarkMemoryCache_Get measures the performance of Get operations.
3130
func BenchmarkMemoryCache_Get(b *testing.B) {
3231
cache := cache.NewMemory(0)
3332
ctx := context.Background()
@@ -45,7 +44,7 @@ func BenchmarkMemoryCache_Get(b *testing.B) {
4544
})
4645
}
4746

48-
// BenchmarkMemoryCache_SetAndGet measures the performance of Set followed by Get
47+
// BenchmarkMemoryCache_SetAndGet measures the performance of Set followed by Get.
4948
func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
5049
cache := cache.NewMemory(0)
5150
ctx := context.Background()
@@ -64,7 +63,7 @@ func BenchmarkMemoryCache_SetAndGet(b *testing.B) {
6463
})
6564
}
6665

67-
// BenchmarkMemoryCache_SetOrFail measures the performance of SetOrFail operations
66+
// BenchmarkMemoryCache_SetOrFail measures the performance of SetOrFail operations.
6867
func BenchmarkMemoryCache_SetOrFail(b *testing.B) {
6968
cache := cache.NewMemory(0)
7069
ctx := context.Background()
@@ -79,7 +78,7 @@ func BenchmarkMemoryCache_SetOrFail(b *testing.B) {
7978
})
8079
}
8180

82-
// BenchmarkMemoryCache_GetAndDelete measures the performance of GetAndDelete operations
81+
// BenchmarkMemoryCache_GetAndDelete measures the performance of GetAndDelete operations.
8382
func BenchmarkMemoryCache_GetAndDelete(b *testing.B) {
8483
cache := cache.NewMemory(0)
8584
ctx := context.Background()
@@ -98,7 +97,7 @@ func BenchmarkMemoryCache_GetAndDelete(b *testing.B) {
9897
})
9998
}
10099

101-
// BenchmarkMemoryCache_Delete measures the performance of Delete operations
100+
// BenchmarkMemoryCache_Delete measures the performance of Delete operations.
102101
func BenchmarkMemoryCache_Delete(b *testing.B) {
103102
cache := cache.NewMemory(0)
104103
ctx := context.Background()
@@ -117,7 +116,7 @@ func BenchmarkMemoryCache_Delete(b *testing.B) {
117116
})
118117
}
119118

120-
// BenchmarkMemoryCache_Cleanup measures the performance of Cleanup operations
119+
// BenchmarkMemoryCache_Cleanup measures the performance of Cleanup operations.
121120
func BenchmarkMemoryCache_Cleanup(b *testing.B) {
122121
cache := cache.NewMemory(0)
123122
ctx := context.Background()
@@ -137,7 +136,7 @@ func BenchmarkMemoryCache_Cleanup(b *testing.B) {
137136
})
138137
}
139138

140-
// BenchmarkMemoryCache_Drain measures the performance of Drain operations
139+
// BenchmarkMemoryCache_Drain measures the performance of Drain operations.
141140
func BenchmarkMemoryCache_Drain(b *testing.B) {
142141
cache := cache.NewMemory(0)
143142
ctx := context.Background()
@@ -157,7 +156,7 @@ func BenchmarkMemoryCache_Drain(b *testing.B) {
157156
})
158157
}
159158

160-
// BenchmarkMemoryCache_ConcurrentReads measures performance with different numbers of concurrent readers
159+
// BenchmarkMemoryCache_ConcurrentReads measures performance with different numbers of concurrent readers.
161160
func BenchmarkMemoryCache_ConcurrentReads(b *testing.B) {
162161
cache := cache.NewMemory(0)
163162
ctx := context.Background()
@@ -190,7 +189,7 @@ func BenchmarkMemoryCache_ConcurrentReads(b *testing.B) {
190189
}
191190
}
192191

193-
// BenchmarkMemoryCache_ConcurrentWrites measures performance with different numbers of concurrent writers
192+
// BenchmarkMemoryCache_ConcurrentWrites measures performance with different numbers of concurrent writers.
194193
func BenchmarkMemoryCache_ConcurrentWrites(b *testing.B) {
195194
cache := cache.NewMemory(0)
196195
ctx := context.Background()
@@ -223,7 +222,7 @@ func BenchmarkMemoryCache_ConcurrentWrites(b *testing.B) {
223222
}
224223
}
225224

226-
// BenchmarkMemoryCache_MixedWorkload measures performance with mixed read/write operations
225+
// BenchmarkMemoryCache_MixedWorkload measures performance with mixed read/write operations.
227226
func BenchmarkMemoryCache_MixedWorkload(b *testing.B) {
228227
cache := cache.NewMemory(0)
229228
ctx := context.Background()
@@ -265,7 +264,7 @@ func BenchmarkMemoryCache_MixedWorkload(b *testing.B) {
265264
}
266265
}
267266

268-
// BenchmarkMemoryCache_Scaling measures how performance scales with increasing load
267+
// BenchmarkMemoryCache_Scaling measures how performance scales with increasing load.
269268
func BenchmarkMemoryCache_Scaling(b *testing.B) {
270269
cache := cache.NewMemory(0)
271270
ctx := context.Background()
@@ -284,7 +283,7 @@ func BenchmarkMemoryCache_Scaling(b *testing.B) {
284283
for _, bm := range benchmarks {
285284
b.Run(bm.name, func(b *testing.B) {
286285
// Pre-populate cache
287-
for i := 0; i < bm.operationsPerGoroutine*bm.goroutines; i++ {
286+
for i := range bm.operationsPerGoroutine * bm.goroutines {
288287
key := "key-" + strconv.Itoa(i)
289288
value := "value-" + strconv.Itoa(i)
290289
cache.Set(ctx, key, []byte(value))
@@ -305,7 +304,7 @@ func BenchmarkMemoryCache_Scaling(b *testing.B) {
305304
}
306305
}
307306

308-
// BenchmarkMemoryCache_TTLOverhead measures the performance impact of TTL operations
307+
// BenchmarkMemoryCache_TTLOverhead measures the performance impact of TTL operations.
309308
func BenchmarkMemoryCache_TTLOverhead(b *testing.B) {
310309
c := cache.NewMemory(0)
311310
ctx := context.Background()
@@ -337,7 +336,7 @@ func BenchmarkMemoryCache_TTLOverhead(b *testing.B) {
337336
}
338337
}
339338

340-
// BenchmarkMemoryCache_LargeValues measures performance with large values
339+
// BenchmarkMemoryCache_LargeValues measures performance with large values.
341340
func BenchmarkMemoryCache_LargeValues(b *testing.B) {
342341
cache := cache.NewMemory(0)
343342
ctx := context.Background()
@@ -375,7 +374,7 @@ func BenchmarkMemoryCache_LargeValues(b *testing.B) {
375374
}
376375
}
377376

378-
// BenchmarkMemoryCache_MemoryGrowth measures memory allocation patterns
377+
// BenchmarkMemoryCache_MemoryGrowth measures memory allocation patterns.
379378
func BenchmarkMemoryCache_MemoryGrowth(b *testing.B) {
380379
cache := cache.NewMemory(0)
381380
ctx := context.Background()
@@ -402,7 +401,7 @@ func BenchmarkMemoryCache_MemoryGrowth(b *testing.B) {
402401
}
403402
}
404403

405-
// BenchmarkMemoryCache_RandomAccess measures performance with random key access patterns
404+
// BenchmarkMemoryCache_RandomAccess measures performance with random key access patterns.
406405
func BenchmarkMemoryCache_RandomAccess(b *testing.B) {
407406
cache := cache.NewMemory(0)
408407
ctx := context.Background()
@@ -426,7 +425,7 @@ func BenchmarkMemoryCache_RandomAccess(b *testing.B) {
426425
})
427426
}
428427

429-
// BenchmarkMemoryCache_HotKey measures performance with a frequently accessed key
428+
// BenchmarkMemoryCache_HotKey measures performance with a frequently accessed key.
430429
func BenchmarkMemoryCache_HotKey(b *testing.B) {
431430
cache := cache.NewMemory(0)
432431
ctx := context.Background()
@@ -444,14 +443,14 @@ func BenchmarkMemoryCache_HotKey(b *testing.B) {
444443
})
445444
}
446445

447-
// BenchmarkMemoryCache_ColdKey measures performance with rarely accessed keys
446+
// BenchmarkMemoryCache_ColdKey measures performance with rarely accessed keys.
448447
func BenchmarkMemoryCache_ColdKey(b *testing.B) {
449448
cache := cache.NewMemory(0)
450449
ctx := context.Background()
451450
const numKeys = 10000
452451

453452
// Pre-populate cache with many keys
454-
for i := 0; i < numKeys; i++ {
453+
for i := range numKeys {
455454
key := "key-" + strconv.Itoa(i)
456455
value := "value-" + strconv.Itoa(i)
457456
cache.Set(ctx, key, []byte(value))

0 commit comments

Comments
 (0)