|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Common errors for numeric operations |
| 10 | +var ( |
| 11 | + ErrNotNumeric = errors.New("value is not numeric") |
| 12 | + ErrOverflow = errors.New("operation would result in overflow") |
| 13 | + ErrUnderflow = errors.New("operation would result in underflow") |
| 14 | + ErrKeyNotFound = errors.New("key not found") |
| 15 | + ErrInvalidAmount = errors.New("invalid increment/decrement amount") |
| 16 | +) |
| 17 | + |
| 18 | +// NumericOperations provides additional numeric operations for the cache |
| 19 | +type NumericOperations struct { |
| 20 | + cache *Cache |
| 21 | +} |
| 22 | + |
| 23 | +// NewNumericOperations creates a new NumericOperations instance |
| 24 | +func NewNumericOperations(cache *Cache) *NumericOperations { |
| 25 | + return &NumericOperations{ |
| 26 | + cache: cache, |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// INCR increments the value of a key by an amount |
| 31 | +// If the key does not exist, it's initialized with 0 before incrementing |
| 32 | +// Returns the new value after incrementing or an error |
| 33 | +func (n *NumericOperations) INCR(key string, amount int64) (int64, error) { |
| 34 | + if amount == 0 { |
| 35 | + return 0, ErrInvalidAmount |
| 36 | + } |
| 37 | + |
| 38 | + shard := n.cache.getShard(key) |
| 39 | + shard.mu.Lock() |
| 40 | + defer shard.mu.Unlock() |
| 41 | + |
| 42 | + // Get current value or initialize with 0 |
| 43 | + var currentValue int64 = 0 |
| 44 | + entry, exists := shard.data[key] |
| 45 | + |
| 46 | + if exists { |
| 47 | + // Check if the value is a valid number |
| 48 | + val, err := strconv.ParseInt(entry.Value, 10, 64) |
| 49 | + if err != nil { |
| 50 | + return 0, ErrNotNumeric |
| 51 | + } |
| 52 | + currentValue = val |
| 53 | + } |
| 54 | + |
| 55 | + // Check for potential overflow |
| 56 | + if (amount > 0 && currentValue > (1<<63-1)-amount) || |
| 57 | + (amount < 0 && currentValue < (-1<<63)+amount) { |
| 58 | + return 0, ErrOverflow |
| 59 | + } |
| 60 | + |
| 61 | + // Calculate new value |
| 62 | + newValue := currentValue + amount |
| 63 | + |
| 64 | + // Update the cache |
| 65 | + shard.data[key] = CacheEntry{ |
| 66 | + Value: strconv.FormatInt(newValue, 10), |
| 67 | + ExpireAt: entry.ExpireAt, // Preserve TTL if it exists |
| 68 | + } |
| 69 | + |
| 70 | + return newValue, nil |
| 71 | +} |
| 72 | + |
| 73 | +// DECR decrements the value of a key by an amount |
| 74 | +// If the key does not exist, it's initialized with 0 before decrementing |
| 75 | +// Returns the new value after decrementing or an error |
| 76 | +func (n *NumericOperations) DECR(key string, amount int64) (int64, error) { |
| 77 | + // Reuse INCR logic with negative amount |
| 78 | + if amount <= 0 { |
| 79 | + return 0, ErrInvalidAmount |
| 80 | + } |
| 81 | + return n.INCR(key, -amount) |
| 82 | +} |
| 83 | + |
| 84 | +// IncrBy increments the value of a key by the specified amount |
| 85 | +// Returns the new value or an error |
| 86 | +func (n *NumericOperations) IncrBy(key string, amount int64) (int64, error) { |
| 87 | + return n.INCR(key, amount) |
| 88 | +} |
| 89 | + |
| 90 | +// DecrBy decrements the value of a key by the specified amount |
| 91 | +// Returns the new value or an error |
| 92 | +func (n *NumericOperations) DecrBy(key string, amount int64) (int64, error) { |
| 93 | + return n.DECR(key, amount) |
| 94 | +} |
| 95 | + |
| 96 | +// Incr increments the value of a key by 1 |
| 97 | +// Returns the new value or an error |
| 98 | +func (n *NumericOperations) Incr(key string) (int64, error) { |
| 99 | + return n.INCR(key, 1) |
| 100 | +} |
| 101 | + |
| 102 | +// Decr decrements the value of a key by 1 |
| 103 | +// Returns the new value or an error |
| 104 | +func (n *NumericOperations) Decr(key string) (int64, error) { |
| 105 | + return n.DECR(key, 1) |
| 106 | +} |
| 107 | + |
| 108 | +// UpdateExpiration updates the expiration time of a key |
| 109 | +// Returns true if the key exists and its expiration was updated |
| 110 | +func (n *NumericOperations) UpdateExpiration(key string, ttl time.Duration) bool { |
| 111 | + shard := n.cache.getShard(key) |
| 112 | + shard.mu.Lock() |
| 113 | + defer shard.mu.Unlock() |
| 114 | + |
| 115 | + entry, exists := shard.data[key] |
| 116 | + if !exists { |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + var expireAt int64 |
| 121 | + if ttl > 0 { |
| 122 | + expireAt = time.Now().Add(ttl).UnixNano() |
| 123 | + } |
| 124 | + |
| 125 | + entry.ExpireAt = expireAt |
| 126 | + shard.data[key] = entry |
| 127 | + |
| 128 | + return true |
| 129 | +} |
0 commit comments