| 1 | package gcachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/ |
| 2 | |
| 3 | import ( |
| 4 | "timescip-go gomod github.com/golang/go/src go1.15 time/" |
| 5 | ) |
| 6 | |
| 7 | // SimpleCache has no clear priority for evict cache. It depends on key-value map order. |
| 8 | type SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache# struct { |
| 9 | baseCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#baseCache. |
| 10 | itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. map[interface{}]*simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem# |
| 11 | } |
| 12 | |
| 13 | func newSimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/newSimpleCache().(cb *CacheBuilderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/CacheBuilder#) *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache# { |
| 14 | c := &SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#{} |
| 15 | buildCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/buildCache().(&c.baseCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#baseCache., cb) |
| 16 | |
| 17 | c.initscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#init().() |
| 18 | c.loadGroupscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#loadGroup..cachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Group#cache. = c |
| 19 | return c |
| 20 | } |
| 21 | |
| 22 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) initscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#init().() { |
| 23 | if c.sizescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#size. <= 0 { |
| 24 | c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. = make(map[interface{}]*simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#) |
| 25 | } else { |
| 26 | c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. = make(map[interface{}]*simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#, c.sizescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#size.) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Set a new key-value pair |
| 31 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Set().(key, value interface{}) error { |
| 32 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 33 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 34 | _, err := c.setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#set().(key, value) |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // Set a new key-value pair with an expiration time |
| 39 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) SetWithExpirescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#SetWithExpire().(key, value interface{}, expiration timescip-go gomod github.com/golang/go/src go1.15 time/.Durationscip-go gomod github.com/golang/go/src go1.15 time/Duration#) error { |
| 40 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 41 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 42 | item, err := c.setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#set().(key, value) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | t := c.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#clock..Nowscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock#Now.().Addscip-go gomod github.com/golang/go/src go1.15 time/Time#Add().(expiration) |
| 48 | item.(*simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#).expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. = &t |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#set().(key, value interface{}) (interface{}, error) { |
| 53 | var err error |
| 54 | if c.serializeFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#serializeFunc. != nil { |
| 55 | value, err = c.serializeFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#serializeFunc.(key, value) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Check for existing item |
| 62 | item, ok := c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.[key] |
| 63 | if ok { |
| 64 | item.valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value. = value |
| 65 | } else { |
| 66 | // Verify size not exceeded |
| 67 | if (len(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.) >= c.sizescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#size.) && c.sizescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#size. > 0 { |
| 68 | c.evictscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#evict().(1) |
| 69 | } |
| 70 | item = &simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#{ |
| 71 | clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#clock.: c.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#clock., |
| 72 | valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value.: value, |
| 73 | } |
| 74 | c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.[key] = item |
| 75 | } |
| 76 | |
| 77 | if c.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#expiration. != nil { |
| 78 | t := c.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#clock..Nowscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock#Now.().Addscip-go gomod github.com/golang/go/src go1.15 time/Time#Add().(*c.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#expiration.) |
| 79 | item.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. = &t |
| 80 | } |
| 81 | |
| 82 | if c.addedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#addedFunc. != nil { |
| 83 | c.addedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#addedFunc.(key, value) |
| 84 | } |
| 85 | |
| 86 | return item, nil |
| 87 | } |
| 88 | |
| 89 | // Get a value from cache pool using key if it exists. |
| 90 | // If it dose not exists key and has LoaderFunc, |
| 91 | // generate a value using `LoaderFunc` method returns value. |
| 92 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Getscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Get().(key interface{}) (interface{}, error) { |
| 93 | v, err := c.getscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#get().(key, false) |
| 94 | if err == KeyNotFoundErrorscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/KeyNotFoundError. { |
| 95 | return c.getWithLoaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#getWithLoader().(key, true) |
| 96 | } |
| 97 | return v, err |
| 98 | } |
| 99 | |
| 100 | // GetIFPresent gets a value from cache pool using key if it exists. |
| 101 | // If it dose not exists key, returns KeyNotFoundError. |
| 102 | // And send a request which refresh value for specified key if cache object has LoaderFunc. |
| 103 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) GetIFPresentscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#GetIFPresent().(key interface{}) (interface{}, error) { |
| 104 | v, err := c.getscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#get().(key, false) |
| 105 | if err == KeyNotFoundErrorscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/KeyNotFoundError. { |
| 106 | return c.getWithLoaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#getWithLoader().(key, false) |
| 107 | } |
| 108 | return v, nil |
| 109 | } |
| 110 | |
| 111 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) getscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#get().(key interface{}, onLoad bool) (interface{}, error) { |
| 112 | v, err := c.getValuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#getValue().(key, onLoad) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if c.deserializeFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#deserializeFunc. != nil { |
| 117 | return c.deserializeFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#deserializeFunc.(key, v) |
| 118 | } |
| 119 | return v, nil |
| 120 | } |
| 121 | |
| 122 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) getValuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#getValue().(key interface{}, onLoad bool) (interface{}, error) { |
| 123 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 124 | item, ok := c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.[key] |
| 125 | if ok { |
| 126 | if !item.IsExpiredscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#IsExpired().(nil) { |
| 127 | v := item.valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value. |
| 128 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 129 | if !onLoad { |
| 130 | c.statsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#stats..IncrHitCountscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/stats#IncrHitCount().() |
| 131 | } |
| 132 | return v, nil |
| 133 | } |
| 134 | c.removescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#remove().(key) |
| 135 | } |
| 136 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 137 | if !onLoad { |
| 138 | c.statsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#stats..IncrMissCountscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/stats#IncrMissCount().() |
| 139 | } |
| 140 | return nil, KeyNotFoundErrorscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/KeyNotFoundError. |
| 141 | } |
| 142 | |
| 143 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) getWithLoaderscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#getWithLoader().(key interface{}, isWait bool) (interface{}, error) { |
| 144 | if c.loaderExpireFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#loaderExpireFunc. == nil { |
| 145 | return nil, KeyNotFoundErrorscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/KeyNotFoundError. |
| 146 | } |
| 147 | value, _, err := c.loadscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#load().(key, func(v interface{}, expiration *timescip-go gomod github.com/golang/go/src go1.15 time/.Durationscip-go gomod github.com/golang/go/src go1.15 time/Duration#, e error) (interface{}, error) { |
| 148 | if e != nil { |
| 149 | return nil, e |
| 150 | } |
| 151 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 152 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 153 | item, err := c.setscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#set().(key, v) |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | if expiration != nil { |
| 158 | t := c.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#clock..Nowscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock#Now.().Addscip-go gomod github.com/golang/go/src go1.15 time/Time#Add().(*expiration) |
| 159 | item.(*simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#).expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. = &t |
| 160 | } |
| 161 | return v, nil |
| 162 | }, isWait) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | return value, nil |
| 167 | } |
| 168 | |
| 169 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) evictscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#evict().(count int) { |
| 170 | now := c.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#clock..Nowscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock#Now.() |
| 171 | current := 0 |
| 172 | for key, item := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 173 | if current >= count { |
| 174 | return |
| 175 | } |
| 176 | if item.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. == nil || now.Afterscip-go gomod github.com/golang/go/src go1.15 time/Time#After().(*item.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration.) { |
| 177 | defer c.removescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#remove().(key) |
| 178 | current++ |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Has checks if key exists in cache |
| 184 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Has().(key interface{}) bool { |
| 185 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RLockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RLock().() |
| 186 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RUnlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RUnlock().() |
| 187 | now := timescip-go gomod github.com/golang/go/src go1.15 time/.Nowscip-go gomod github.com/golang/go/src go1.15 time/Now().() |
| 188 | return c.hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#has().(key, &now) |
| 189 | } |
| 190 | |
| 191 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#has().(key interface{}, now *timescip-go gomod github.com/golang/go/src go1.15 time/.Timescip-go gomod github.com/golang/go/src go1.15 time/Time#) bool { |
| 192 | item, ok := c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.[key] |
| 193 | if !ok { |
| 194 | return false |
| 195 | } |
| 196 | return !item.IsExpiredscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#IsExpired().(now) |
| 197 | } |
| 198 | |
| 199 | // Remove removes the provided key from the cache. |
| 200 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Removescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Remove().(key interface{}) bool { |
| 201 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 202 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 203 | |
| 204 | return c.removescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#remove().(key) |
| 205 | } |
| 206 | |
| 207 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) removescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#remove().(key interface{}) bool { |
| 208 | item, ok := c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.[key] |
| 209 | if ok { |
| 210 | delete(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items., key) |
| 211 | if c.evictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#evictedFunc. != nil { |
| 212 | c.evictedFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#evictedFunc.(key, item.valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value.) |
| 213 | } |
| 214 | return true |
| 215 | } |
| 216 | return false |
| 217 | } |
| 218 | |
| 219 | // Returns a slice of the keys in the cache. |
| 220 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) keysscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#keys().() []interface{} { |
| 221 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RLockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RLock().() |
| 222 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RUnlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RUnlock().() |
| 223 | keys := make([]interface{}, len(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.)) |
| 224 | var i = 0 |
| 225 | for k := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 226 | keys[i] = k |
| 227 | i++ |
| 228 | } |
| 229 | return keys |
| 230 | } |
| 231 | |
| 232 | // GetALL returns all key-value pairs in the cache. |
| 233 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) GetALLscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#GetALL().(checkExpired bool) map[interface{}]interface{} { |
| 234 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RLockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RLock().() |
| 235 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RUnlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RUnlock().() |
| 236 | items := make(map[interface{}]interface{}, len(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.)) |
| 237 | now := timescip-go gomod github.com/golang/go/src go1.15 time/.Nowscip-go gomod github.com/golang/go/src go1.15 time/Now().() |
| 238 | for k, item := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 239 | if !checkExpired || c.hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#has().(k, &now) { |
| 240 | items[k] = item.valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value. |
| 241 | } |
| 242 | } |
| 243 | return items |
| 244 | } |
| 245 | |
| 246 | // Keys returns a slice of the keys in the cache. |
| 247 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Keysscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Keys().(checkExpired bool) []interface{} { |
| 248 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RLockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RLock().() |
| 249 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RUnlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RUnlock().() |
| 250 | keys := make([]interface{}, 0, len(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.)) |
| 251 | now := timescip-go gomod github.com/golang/go/src go1.15 time/.Nowscip-go gomod github.com/golang/go/src go1.15 time/Now().() |
| 252 | for k := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 253 | if !checkExpired || c.hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#has().(k, &now) { |
| 254 | keys = append(keys, k) |
| 255 | } |
| 256 | } |
| 257 | return keys |
| 258 | } |
| 259 | |
| 260 | // Len returns the number of items in the cache. |
| 261 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Lenscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Len().(checkExpired bool) int { |
| 262 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RLockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RLock().() |
| 263 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..RUnlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#RUnlock().() |
| 264 | if !checkExpired { |
| 265 | return len(c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items.) |
| 266 | } |
| 267 | var length int |
| 268 | now := timescip-go gomod github.com/golang/go/src go1.15 time/.Nowscip-go gomod github.com/golang/go/src go1.15 time/Now().() |
| 269 | for k := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 270 | if c.hasscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#has().(k, &now) { |
| 271 | length++ |
| 272 | } |
| 273 | } |
| 274 | return length |
| 275 | } |
| 276 | |
| 277 | // Completely clear the cache |
| 278 | func (c *SimpleCachescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#) Purgescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#Purge().() { |
| 279 | c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Lockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Lock().() |
| 280 | defer c.muscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.15 sync/RWMutex#Unlock().() |
| 281 | |
| 282 | if c.purgeVisitorFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#purgeVisitorFunc. != nil { |
| 283 | for key, item := range c.itemsscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#items. { |
| 284 | c.purgeVisitorFuncscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/baseCache#purgeVisitorFunc.(key, item.valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value.) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | c.initscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/SimpleCache#init().() |
| 289 | } |
| 290 | |
| 291 | type simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem# struct { |
| 292 | clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#clock. Clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock# |
| 293 | valuescip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#value. interface{} |
| 294 | expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. *timescip-go gomod github.com/golang/go/src go1.15 time/.Timescip-go gomod github.com/golang/go/src go1.15 time/Time# |
| 295 | } |
| 296 | |
| 297 | // IsExpired returns boolean value whether this item is expired or not. |
| 298 | func (si *simpleItemscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#) IsExpiredscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#IsExpired().(now *timescip-go gomod github.com/golang/go/src go1.15 time/.Timescip-go gomod github.com/golang/go/src go1.15 time/Time#) bool { |
| 299 | if si.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration. == nil { |
| 300 | return false |
| 301 | } |
| 302 | if now == nil { |
| 303 | t := si.clockscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#clock..Nowscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/Clock#Now.() |
| 304 | now = &t |
| 305 | } |
| 306 | return si.expirationscip-go gomod github.com/bluele/gcache . `github.com/bluele/gcache`/simpleItem#expiration..Beforescip-go gomod github.com/golang/go/src go1.15 time/Time#Before().(*now) |
| 307 | } |