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

Method Replace

cache.go:106–116  ·  view source on GitHub ↗

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

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

Source from the content-addressed store, hash-verified

104// Set a new value for the cache key only if it already exists, and the existing
105// item hasn't expired. Returns an error otherwise.
106func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
107 c.mu.Lock()
108 _, found := c.get(k)
109 if !found {
110 c.mu.Unlock()
111 return fmt.Errorf("Item %s doesn't exist", k)
112 }
113 c.set(k, x, d)
114 c.mu.Unlock()
115 return nil
116}
117
118// Get an item from the cache. Returns the item or nil, and a bool indicating
119// whether the key was found.

Callers 1

TestReplaceFunction · 0.45

Calls 2

getMethod · 0.95
setMethod · 0.95

Tested by 1

TestReplaceFunction · 0.36