MCPcopy Create free account
hub / github.com/patrickmn/go-cache / Set

Method Set

cache.go:51–68  ·  view source on GitHub ↗

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

(k string, x interface{}, d time.Duration)

Source from the content-addressed store, hash-verified

49// (DefaultExpiration), the cache's default expiration time is used. If it is -1
50// (NoExpiration), the item never expires.
51func (c *cache) Set(k string, x interface{}, d time.Duration) {
52 // "Inlining" of set
53 var e int64
54 if d == DefaultExpiration {
55 d = c.defaultExpiration
56 }
57 if d > 0 {
58 e = time.Now().Add(d).UnixNano()
59 }
60 c.mu.Lock()
61 c.items[k] = Item{
62 Object: x,
63 Expiration: e,
64 }
65 // TODO: Calls to mu.Unlock are currently not deferred because defer
66 // adds ~200 ns (as of go1.)
67 c.mu.Unlock()
68}
69
70func (c *cache) set(k string, x interface{}, d time.Duration) {
71 var e int64

Callers 15

SetDefaultMethod · 0.95
TestShardedCacheFunction · 0.45
benchmarkShardedCacheGetFunction · 0.45
TestCacheFunction · 0.45
TestCacheTimesFunction · 0.45
TestStorePointerToStructFunction · 0.45
TestIncrementWithIntFunction · 0.45
TestIncrementWithInt8Function · 0.45
TestIncrementWithInt16Function · 0.45
TestIncrementWithInt32Function · 0.45
TestIncrementWithInt64Function · 0.45

Calls 1

AddMethod · 0.45

Tested by 15

TestShardedCacheFunction · 0.36
benchmarkShardedCacheGetFunction · 0.36
TestCacheFunction · 0.36
TestCacheTimesFunction · 0.36
TestStorePointerToStructFunction · 0.36
TestIncrementWithIntFunction · 0.36
TestIncrementWithInt8Function · 0.36
TestIncrementWithInt16Function · 0.36
TestIncrementWithInt32Function · 0.36
TestIncrementWithInt64Function · 0.36
TestIncrementWithUintFunction · 0.36