| 1 | package gocache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "time" |
| 6 | "unsafe" |
| 7 | ) |
| 8 | |
| 9 | // Entry is a cache entry |
| 10 | type Entry struct { |
| 11 | // Key is the name of the cache entry |
| 12 | Key string |
| 13 | |
| 14 | // Value is the value of the cache entry |
| 15 | Value any |
| 16 | |
| 17 | // RelevantTimestamp is the variable used to store either: |
| 18 | // - creation timestamp, if the Cache's EvictionPolicy is FirstInFirstOut |
| 19 | // - last access timestamp, if the Cache's EvictionPolicy is LeastRecentlyUsed |
| 20 | // |
| 21 | // Note that updating an existing entry will also update this value |
| 22 | RelevantTimestamp time.Time |
| 23 | |
| 24 | // Expiration is the unix time in nanoseconds at which the entry will expire (-1 means no expiration) |
| 25 | Expiration int64 |
| 26 | |
| 27 | next *Entry |
| 28 | previous *Entry |
| 29 | } |
| 30 | |
| 31 | // Accessed updates the Entry's RelevantTimestamp to now |
| 32 | func (entry *Entry) Accessed() { |
| 33 | entry.RelevantTimestamp = time.Now() |
| 34 | } |
| 35 | |
| 36 | // Expired returns whether the Entry has expired |
| 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 | // SizeInBytes returns the size of an entry in bytes, approximately. |
| 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 | } |