MCPcopy Index your code
hub / github.com/upper/db / Write

Method Write

internal/cache/cache.go:103–125  ·  view source on GitHub ↗

Write stores a value in memory. If the value already exists its overwritten.

(h Hashable, value interface{})

Source from the content-addressed store, hash-verified

101
102// Write stores a value in memory. If the value already exists its overwritten.
103func (c *Cache) Write(h Hashable, value interface{}) {
104 c.mu.Lock()
105 defer c.mu.Unlock()
106
107 key := h.Hash()
108
109 if item, ok := c.items[key]; ok {
110 item.Value.(*cacheItem).value = value
111 c.keys.MoveToFront(item)
112 return
113 }
114
115 c.items[key] = c.keys.PushFront(&cacheItem{key, value})
116
117 for c.keys.Len() > c.capacity {
118 item := c.keys.Remove(c.keys.Back()).(*cacheItem)
119 delete(c.items, item.key)
120
121 if evictor, hasOnEvict := item.value.(HasOnEvict); hasOnEvict {
122 evictor.OnEvict()
123 }
124 }
125}
126
127// Clear generates a new memory space, leaving the old memory unreferenced, so
128// it can be claimed by the garbage collector.

Callers 15

TestCacheFunction · 0.95
BenchmarkWriteSameValueFunction · 0.95
BenchmarkWriteNewValueFunction · 0.95
PrimaryKeysMethod · 0.80
CollectionMethod · 0.80
prepareStatementMethod · 0.80
CompileMethod · 0.80
CompileMethod · 0.80
CompileMethod · 0.80
CompileMethod · 0.80
CompileMethod · 0.80

Calls 3

HashMethod · 0.65
OnEvictMethod · 0.65
LenMethod · 0.45

Tested by 4

TestCacheFunction · 0.76
BenchmarkWriteSameValueFunction · 0.76
BenchmarkWriteNewValueFunction · 0.76