Skip to content

Commit c32fabb

Browse files
authored
chore: update maxCost to be in mb for better readibility (#218)
1 parent 76afcc4 commit c32fabb

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

pkg/zcache/config.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import (
99
"github.com/zondax/golem/pkg/metrics"
1010
)
1111

12+
const (
13+
// Default Ristretto cache config
14+
DefaultNumCounters = int64(1e7) // 10M keys
15+
DefaultMaxCostMB = int64(1024) // 1GB
16+
DefaultBufferItems = int64(64)
17+
)
18+
1219
type StatsMetrics struct {
1320
Enable bool
1421
UpdateInterval time.Duration
1522
}
1623

17-
type CleanupProcess struct {
18-
Interval time.Duration
19-
BatchSize int
20-
ThrottleTime time.Duration
21-
}
22-
2324
type RemoteConfig struct {
2425
Network string
2526
Addr string
@@ -48,7 +49,7 @@ type LocalConfig struct {
4849

4950
// Add Ristretto cache configuration
5051
NumCounters int64 `json:"num_counters"` // default: 1e7
51-
MaxCost int64 `json:"max_cost"` // default: 1 << 30 (1GB)
52+
MaxCostMB int64 `json:"max_cost_mb"` // in MB, default: 1024 (1GB)
5253
BufferItems int64 `json:"buffer_items"` // default: 64
5354
}
5455

@@ -73,17 +74,19 @@ func (c *RemoteConfig) ToRedisConfig() *redis.Options {
7374
func (c *LocalConfig) ToRistrettoConfig() *ristretto.Config {
7475
numCounters := c.NumCounters
7576
if numCounters == 0 {
76-
numCounters = 1e7 // default 10M keys
77+
numCounters = DefaultNumCounters
7778
}
7879

79-
maxCost := c.MaxCost
80-
if maxCost == 0 {
81-
maxCost = 1 << 30 // default 1GB
80+
maxCostMB := c.MaxCostMB
81+
if maxCostMB == 0 {
82+
maxCostMB = DefaultMaxCostMB
8283
}
84+
// Convert MB to bytes
85+
maxCost := maxCostMB << 20
8386

8487
bufferItems := c.BufferItems
8588
if bufferItems == 0 {
86-
bufferItems = 64 // default buffer size
89+
bufferItems = DefaultBufferItems
8790
}
8891

8992
return &ristretto.Config{

pkg/zcache/readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func main() {
6767
config := zcache.LocalConfig{
6868
// Ristretto cache configuration
6969
NumCounters: 1e7, // Number of keys to track frequency (default: 10M)
70-
MaxCost: 1 << 30, // Maximum cost of cache (default: 1GB)
70+
MaxCostMB: 1024, // Maximum cost of cache in MB (default: 1024MB/1GB)
7171
BufferItems: 64, // Number of keys per Get buffer (default: 64)
7272

7373
// Metrics are required
@@ -98,7 +98,7 @@ func main() {
9898
localConfig := zcache.LocalConfig{
9999
// Ristretto cache configuration
100100
NumCounters: 1e7, // Number of keys to track (default: 10M)
101-
MaxCost: 1 << 29, // Max memory usage - 512MB
101+
MaxCostMB: 512, // Max memory usage - 512MB
102102
BufferItems: 64, // Size of Get buffer
103103

104104
// Metrics are required
@@ -203,7 +203,7 @@ When using the local cache (Ristretto), memory is managed efficiently:
203203

204204
2. **Configuration Parameters**:
205205
- `NumCounters`: Number of keys to track (default: 1e7 or 10 million)
206-
- `MaxCost`: Maximum memory cost of cache (default: 1 << 30 or 1GB)
206+
- `MaxCostMB`: Maximum memory in MB (default: 1024MB or 1GB)
207207
- `BufferItems`: Size of the Get buffer for handling concurrent operations (default: 64)
208208

209209
3. **TTL Behavior**:
@@ -231,7 +231,7 @@ For Redis metrics:
231231
### Best Practices
232232
1. **Memory Configuration**:
233233
- Set appropriate `NumCounters` based on expected number of keys (~10x the items)
234-
- Configure `MaxCost` based on available system memory
234+
- Configure `MaxCostMB` based on available system memory (in megabytes)
235235
- Adjust `BufferItems` for high concurrency scenarios (default 64 is suitable for most cases)
236236

237237
2. **Production Recommendations**:
@@ -242,7 +242,7 @@ For Redis metrics:
242242

243243
3. **Cache Tuning**:
244244
- For high-throughput systems, increase `BufferItems`
245-
- For memory-constrained environments, decrease `MaxCost` appropriately
245+
- For memory-constrained environments, decrease `MaxCostMB` appropriately
246246
- Keep `NumCounters` at approximately 10x your expected item count for optimal hit ratio
247247

248248
### Notes

0 commit comments

Comments
 (0)