| 1 |
package gocache |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
type Entry struct { |
| 11 |
|
| 12 |
Key string |
| 13 |
|
| 14 |
|
| 15 |
Value any |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
RelevantTimestamp time.Time |
| 23 |
|
| 24 |
|
| 25 |
Expiration int64 |
| 26 |
|
| 27 |
next *Entry |
| 28 |
previous *Entry |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func (entry *Entry) Accessed() { |
| 33 |
entry.RelevantTimestamp = time.Now() |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func (entry Entry) Expired() bool { |
| 38 |
if entry.Expiration > 0 { |
| 39 |
if time.Now().UnixNano() > entry.Expiration { |
| 40 |
return true |
| 41 |
} |
| 42 |
} |
| 43 |
return false |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (entry *Entry) SizeInBytes() int { |
| 48 |
return toBytes(entry.Key) + toBytes(entry.Value) + 32 |
| 49 |
} |
| 50 |
|
| 51 |
func toBytes(value any) int { |
| 52 |
switch value.(type) { |
| 53 |
case string: |
| 54 |
return int(unsafe.Sizeof(value)) + len(value.(string)) |
| 55 |
case int8, uint8, bool: |
| 56 |
return int(unsafe.Sizeof(value)) + 1 |
| 57 |
case int16, uint16: |
| 58 |
return int(unsafe.Sizeof(value)) + 2 |
| 59 |
case int32, uint32, float32, complex64: |
| 60 |
return int(unsafe.Sizeof(value)) + 4 |
| 61 |
case int64, uint64, int, uint, float64, complex128: |
| 62 |
return int(unsafe.Sizeof(value)) + 8 |
| 63 |
case []any: |
| 64 |
size := 0 |
| 65 |
for _, v := range value.([]any) { |
| 66 |
size += toBytes(v) |
| 67 |
} |
| 68 |
return int(unsafe.Sizeof(value)) + size |
| 69 |
case []string: |
| 70 |
size := 0 |
| 71 |
for _, v := range value.([]string) { |
| 72 |
size += toBytes(v) |
| 73 |
} |
| 74 |
return int(unsafe.Sizeof(value)) + size |
| 75 |
case []int8: |
| 76 |
return int(unsafe.Sizeof(value)) + len(value.([]int8)) |
| 77 |
case []uint8: |
| 78 |
return int(unsafe.Sizeof(value)) + len(value.([]uint8)) |
| 79 |
case []bool: |
| 80 |
return int(unsafe.Sizeof(value)) + len(value.([]bool)) |
| 81 |
case []int16: |
| 82 |
return int(unsafe.Sizeof(value)) + (len(value.([]int16)) * 2) |
| 83 |
case []uint16: |
| 84 |
return int(unsafe.Sizeof(value)) + (len(value.([]uint16)) * 2) |
| 85 |
case []int32: |
| 86 |
return int(unsafe.Sizeof(value)) + (len(value.([]int32)) * 4) |
| 87 |
case []uint32: |
| 88 |
return int(unsafe.Sizeof(value)) + (len(value.([]uint32)) * 4) |
| 89 |
case []float32: |
| 90 |
return int(unsafe.Sizeof(value)) + (len(value.([]float32)) * 4) |
| 91 |
case []complex64: |
| 92 |
return int(unsafe.Sizeof(value)) + (len(value.([]complex64)) * 4) |
| 93 |
case []int64: |
| 94 |
return int(unsafe.Sizeof(value)) + (len(value.([]int64)) * 8) |
| 95 |
case []uint64: |
| 96 |
return int(unsafe.Sizeof(value)) + (len(value.([]uint64)) * 8) |
| 97 |
case []int: |
| 98 |
return int(unsafe.Sizeof(value)) + (len(value.([]int)) * 8) |
| 99 |
case []uint: |
| 100 |
return int(unsafe.Sizeof(value)) + (len(value.([]uint)) * 8) |
| 101 |
case []float64: |
| 102 |
return int(unsafe.Sizeof(value)) + (len(value.([]float64)) * 8) |
| 103 |
case []complex128: |
| 104 |
return int(unsafe.Sizeof(value)) + (len(value.([]complex128)) * 8) |
| 105 |
default: |
| 106 |
return int(unsafe.Sizeof(value)) + len(fmt.Sprintf("%v", value)) |
| 107 |
} |
| 108 |
} |