mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-05 22:53:34 -04:00
Bump github.com/jellydator/ttlcache/v3 from 3.0.1 to 3.1.0
Bumps [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) from 3.0.1 to 3.1.0. - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.0.1...v3.1.0) --- updated-dependencies: - dependency-name: github.com/jellydator/ttlcache/v3 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
3bf5e5efa4
commit
68e846d0ee
2
go.mod
2
go.mod
@@ -48,7 +48,7 @@ require (
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1
|
||||
github.com/jellydator/ttlcache/v2 v2.11.1
|
||||
github.com/jellydator/ttlcache/v3 v3.0.1
|
||||
github.com/jellydator/ttlcache/v3 v3.1.0
|
||||
github.com/justinas/alice v1.2.0
|
||||
github.com/leonelquinteros/gotext v1.5.3-0.20230317130943-71a59c05b2c1
|
||||
github.com/libregraph/idm v0.4.1-0.20230221143410-3503963047a5
|
||||
|
||||
4
go.sum
4
go.sum
@@ -1359,8 +1359,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/jellydator/ttlcache/v2 v2.11.1 h1:AZGME43Eh2Vv3giG6GeqeLeFXxwxn1/qHItqWZl6U64=
|
||||
github.com/jellydator/ttlcache/v2 v2.11.1/go.mod h1:RtE5Snf0/57e+2cLWFYWCCsLas2Hy3c5Z4n14XmSvTI=
|
||||
github.com/jellydator/ttlcache/v3 v3.0.1 h1:cHgCSMS7TdQcoprXnWUptJZzyFsqs18Lt8VVhRuZYVU=
|
||||
github.com/jellydator/ttlcache/v3 v3.0.1/go.mod h1:WwTaEmcXQ3MTjOm4bsZoDFiCu/hMvNWLO1w67RXz6h4=
|
||||
github.com/jellydator/ttlcache/v3 v3.1.0 h1:0gPFG0IHHP6xyUyXq+JaD8fwkDCqgqwohXNJBcYE71g=
|
||||
github.com/jellydator/ttlcache/v3 v3.1.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
|
||||
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
|
||||
|
||||
94
vendor/github.com/jellydator/ttlcache/v3/CHANGELOG.md
generated
vendored
94
vendor/github.com/jellydator/ttlcache/v3/CHANGELOG.md
generated
vendored
@@ -1,94 +0,0 @@
|
||||
# 2.11.0 (December 2021)
|
||||
|
||||
#64: @DoubeDi added a method `GetItems` to retrieve all items in the cache. This method also triggers all callbacks associated with a normal `Get`
|
||||
|
||||
## API changes:
|
||||
|
||||
// GetItems returns a copy of all items in the cache. Returns nil when the cache has been closed.
|
||||
func (cache *Cache) GetItems() map[string]interface{} {
|
||||
|
||||
# 2.10.0 (December 2021)
|
||||
|
||||
#62 : @nikhilk1701 found a memory leak where removed items are not directly eligible for garbage collection. There are no API changes.
|
||||
|
||||
# 2.9.0 (October 2021)
|
||||
|
||||
#55,#56,#57 : @chenyahui was on fire and greatly improved the peformance of the library. He also got rid of the blocking call to expirationNotification, making the code run twice as fast in the benchmarks!
|
||||
|
||||
# 2.8.1 (September 2021)
|
||||
|
||||
#53 : Avoids recalculation of TTL value returned in API when TTL is extended. by @iczc
|
||||
|
||||
# 2.8.0 (August 2021)
|
||||
|
||||
#51 : The call GetWithTTL(key string) (interface{}, time.Duration, error) is added so that you can retrieve an item, and also know the remaining TTL. Thanks to @asgarciap for contributing.
|
||||
|
||||
# 2.7.0 (June 2021)
|
||||
|
||||
#46 : got panic
|
||||
|
||||
A panic occured in a line that checks the maximum amount of items in the cache. While not definite root cause has been found, there is indeed the possibility of crashing an empty cache if the cache limit is set to 'zero' which codes for infinite. This would lead to removal of the first item in the cache which would panic on an empty cache.
|
||||
|
||||
Fixed this by applying the global cache lock to all configuration options as well.
|
||||
|
||||
# 2.6.0 (May 2021)
|
||||
|
||||
#44 : There are no API changes, but a contribution was made to use https://pkg.go.dev/golang.org/x/sync/singleflight as a way to provide everybody waiting for a key with that key when it's fetched.
|
||||
|
||||
This removes some complexity from the code and will make sure that all callers will get a return value even if there's high concurrency and low TTL (as proven by the test that was added).
|
||||
|
||||
# 2.5.0 (May 2021)
|
||||
|
||||
## API changes:
|
||||
|
||||
* #39 : Allow custom loader function for each key via `GetByLoader`
|
||||
|
||||
Introduce the `SimpleCache` interface for quick-start and basic usage.
|
||||
|
||||
# 2.4.0 (April 2021)
|
||||
|
||||
## API changes:
|
||||
|
||||
* #42 : Add option to get list of keys
|
||||
* #40: Allow 'Touch' on items without other operation
|
||||
|
||||
// Touch resets the TTL of the key when it exists, returns ErrNotFound if the key is not present.
|
||||
func (cache *Cache) Touch(key string) error
|
||||
|
||||
// GetKeys returns all keys of items in the cache. Returns nil when the cache has been closed.
|
||||
func (cache *Cache) GetKeys() []string
|
||||
|
||||
# 2.3.0 (February 2021)
|
||||
|
||||
## API changes:
|
||||
|
||||
* #38: Added func (cache *Cache) SetExpirationReasonCallback(callback ExpireReasonCallback) This wil function will replace SetExpirationCallback(..) in the next major version.
|
||||
|
||||
# 2.2.0 (January 2021)
|
||||
|
||||
## API changes:
|
||||
|
||||
* #37 : a GetMetrics call is now available for some information on hits/misses etc.
|
||||
* #34 : Errors are now const
|
||||
|
||||
# 2.1.0 (October 2020)
|
||||
|
||||
## API changes
|
||||
|
||||
* `SetCacheSizeLimit(limit int)` a call was contributed to set a cache limit. #35
|
||||
|
||||
# 2.0.0 (July 2020)
|
||||
|
||||
## Fixes #29, #30, #31
|
||||
|
||||
## Behavioural changes
|
||||
|
||||
* `Remove(key)` now also calls the expiration callback when it's set
|
||||
* `Count()` returns zero when the cache is closed
|
||||
|
||||
## API changes
|
||||
|
||||
* `SetLoaderFunction` allows you to provide a function to retrieve data on missing cache keys.
|
||||
* Operations that affect item behaviour such as `Close`, `Set`, `SetWithTTL`, `Get`, `Remove`, `Purge` now return an error with standard errors `ErrClosed` an `ErrNotFound` instead of a bool or nothing
|
||||
* `SkipTTLExtensionOnHit` replaces `SkipTtlExtensionOnHit` to satisfy golint
|
||||
* The callback types are now exported
|
||||
32
vendor/github.com/jellydator/ttlcache/v3/README.md
generated
vendored
32
vendor/github.com/jellydator/ttlcache/v3/README.md
generated
vendored
@@ -1,4 +1,4 @@
|
||||
[#](#) TTLCache - an in-memory cache with item expiration
|
||||
## TTLCache - an in-memory cache with item expiration and generics
|
||||
|
||||
[](https://pkg.go.dev/github.com/jellydator/ttlcache/v3)
|
||||
[](https://github.com/jellydator/ttlcache/actions/workflows/go.yml)
|
||||
@@ -6,15 +6,14 @@
|
||||
[](https://goreportcard.com/report/github.com/jellydator/ttlcache/v3)
|
||||
|
||||
## Features
|
||||
- Simple API.
|
||||
- Type parameters.
|
||||
- Item expiration and automatic deletion.
|
||||
- Automatic expiration time extension on each `Get` call.
|
||||
- `Loader` interface that is used to load/lazily initialize missing cache
|
||||
items.
|
||||
- Subscription to cache events (insertion and eviction).
|
||||
- Metrics.
|
||||
- Configurability.
|
||||
- Simple API
|
||||
- Type parameters
|
||||
- Item expiration and automatic deletion
|
||||
- Automatic expiration time extension on each `Get` call
|
||||
- `Loader` interface that may be used to load/lazily initialize missing cache
|
||||
items
|
||||
- Event handlers (insertion and eviction)
|
||||
- Metrics
|
||||
|
||||
## Installation
|
||||
```
|
||||
@@ -67,8 +66,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
The data stored in `ttlcache.Cache` can be retrieved and updated with
|
||||
`Set`, `Get`, `Delete`, etc. methods:
|
||||
The data stored in `ttlcache.Cache` can be retrieved, checked and updated with
|
||||
`Set`, `Get`, `Delete`, `Has` etc. methods:
|
||||
```go
|
||||
func main() {
|
||||
cache := ttlcache.New[string, string](
|
||||
@@ -84,10 +83,19 @@ func main() {
|
||||
item := cache.Get("first")
|
||||
fmt.Println(item.Value(), item.ExpiresAt())
|
||||
|
||||
// check key
|
||||
ok := cache.Has("third")
|
||||
|
||||
// delete data
|
||||
cache.Delete("second")
|
||||
cache.DeleteExpired()
|
||||
cache.DeleteAll()
|
||||
|
||||
// retrieve data if in cache otherwise insert data
|
||||
item, retrieved := cache.GetOrSet("fourth", "value4", WithTTL[string, string](ttlcache.DefaultTTL))
|
||||
|
||||
// retrieve and delete data
|
||||
item, present := cache.GetAndDelete("fourth")
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
202
vendor/github.com/jellydator/ttlcache/v3/cache.go
generated
vendored
202
vendor/github.com/jellydator/ttlcache/v3/cache.go
generated
vendored
@@ -76,7 +76,8 @@ func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
|
||||
|
||||
// updateExpirations updates the expiration queue and notifies
|
||||
// the cache auto cleaner if needed.
|
||||
// Not concurrently safe.
|
||||
// Not safe for concurrent use by multiple goroutines without additional
|
||||
// locking.
|
||||
func (c *Cache[K, V]) updateExpirations(fresh bool, elem *list.Element) {
|
||||
var oldExpiresAt time.Time
|
||||
|
||||
@@ -125,7 +126,8 @@ func (c *Cache[K, V]) updateExpirations(fresh bool, elem *list.Element) {
|
||||
}
|
||||
|
||||
// set creates a new item, adds it to the cache and then returns it.
|
||||
// Not concurrently safe.
|
||||
// Not safe for concurrent use by multiple goroutines without additional
|
||||
// locking.
|
||||
func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
if ttl == DefaultTTL {
|
||||
ttl = c.options.ttl
|
||||
@@ -147,7 +149,7 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
}
|
||||
|
||||
// create a new item
|
||||
item := newItem(key, value, ttl)
|
||||
item := newItem(key, value, ttl, c.options.enableVersionTracking)
|
||||
elem = c.items.lru.PushFront(item)
|
||||
c.items.values[key] = elem
|
||||
c.updateExpirations(true, elem)
|
||||
@@ -168,7 +170,8 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
// get retrieves an item from the cache and extends its expiration
|
||||
// time if 'touch' is set to true.
|
||||
// It returns nil if the item is not found or is expired.
|
||||
// Not concurrently safe.
|
||||
// Not safe for concurrent use by multiple goroutines without additional
|
||||
// locking.
|
||||
func (c *Cache[K, V]) get(key K, touch bool) *list.Element {
|
||||
elem := c.items.values[key]
|
||||
if elem == nil {
|
||||
@@ -190,10 +193,57 @@ func (c *Cache[K, V]) get(key K, touch bool) *list.Element {
|
||||
return elem
|
||||
}
|
||||
|
||||
// getWithOpts wraps the get method, applies the given options, and updates
|
||||
// the metrics.
|
||||
// It returns nil if the item is not found or is expired.
|
||||
// If 'lockAndLoad' is set to true, the mutex is locked before calling the
|
||||
// get method and unlocked after it returns. It also indicates that the
|
||||
// loader should be used to load external data when the get method returns
|
||||
// a nil value and the mutex is unlocked.
|
||||
// If 'lockAndLoad' is set to false, neither the mutex nor the loader is
|
||||
// used.
|
||||
func (c *Cache[K, V]) getWithOpts(key K, lockAndLoad bool, opts ...Option[K, V]) *Item[K, V] {
|
||||
getOpts := options[K, V]{
|
||||
loader: c.options.loader,
|
||||
disableTouchOnHit: c.options.disableTouchOnHit,
|
||||
}
|
||||
|
||||
applyOptions(&getOpts, opts...)
|
||||
|
||||
if lockAndLoad {
|
||||
c.items.mu.Lock()
|
||||
}
|
||||
|
||||
elem := c.get(key, !getOpts.disableTouchOnHit)
|
||||
|
||||
if lockAndLoad {
|
||||
c.items.mu.Unlock()
|
||||
}
|
||||
|
||||
if elem == nil {
|
||||
c.metricsMu.Lock()
|
||||
c.metrics.Misses++
|
||||
c.metricsMu.Unlock()
|
||||
|
||||
if lockAndLoad && getOpts.loader != nil {
|
||||
return getOpts.loader.Load(c, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
c.metricsMu.Lock()
|
||||
c.metrics.Hits++
|
||||
c.metricsMu.Unlock()
|
||||
|
||||
return elem.Value.(*Item[K, V])
|
||||
}
|
||||
|
||||
// evict deletes items from the cache.
|
||||
// If no items are provided, all currently present cache items
|
||||
// are evicted.
|
||||
// Not concurrently safe.
|
||||
// Not safe for concurrent use by multiple goroutines without additional
|
||||
// locking.
|
||||
func (c *Cache[K, V]) evict(reason EvictionReason, elems ...*list.Element) {
|
||||
if len(elems) > 0 {
|
||||
c.metricsMu.Lock()
|
||||
@@ -235,9 +285,27 @@ func (c *Cache[K, V]) evict(reason EvictionReason, elems ...*list.Element) {
|
||||
c.items.expQueue = newExpirationQueue[K, V]()
|
||||
}
|
||||
|
||||
// delete deletes an item by the provided key.
|
||||
// The method is no-op if the item is not found.
|
||||
// Not safe for concurrent use by multiple goroutines without additional
|
||||
// locking.
|
||||
func (c *Cache[K, V]) delete(key K) {
|
||||
elem := c.items.values[key]
|
||||
if elem == nil {
|
||||
return
|
||||
}
|
||||
|
||||
c.evict(EvictionReasonDeleted, elem)
|
||||
}
|
||||
|
||||
// Set creates a new item from the provided key and value, adds
|
||||
// it to the cache and then returns it. If an item associated with the
|
||||
// provided key already exists, the new item overwrites the existing one.
|
||||
// NoTTL constant or -1 can be used to indicate that the item should never
|
||||
// expire.
|
||||
// DefaultTTL constant or 0 can be used to indicate that the item should use
|
||||
// the default/global TTL that was specified when the cache instance was
|
||||
// created.
|
||||
func (c *Cache[K, V]) Set(key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
c.items.mu.Lock()
|
||||
defer c.items.mu.Unlock()
|
||||
@@ -250,34 +318,7 @@ func (c *Cache[K, V]) Set(key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
// expiration timestamp on successful retrieval.
|
||||
// If the item is not found, a nil value is returned.
|
||||
func (c *Cache[K, V]) Get(key K, opts ...Option[K, V]) *Item[K, V] {
|
||||
getOpts := options[K, V]{
|
||||
loader: c.options.loader,
|
||||
disableTouchOnHit: c.options.disableTouchOnHit,
|
||||
}
|
||||
|
||||
applyOptions(&getOpts, opts...)
|
||||
|
||||
c.items.mu.Lock()
|
||||
elem := c.get(key, !getOpts.disableTouchOnHit)
|
||||
c.items.mu.Unlock()
|
||||
|
||||
if elem == nil {
|
||||
c.metricsMu.Lock()
|
||||
c.metrics.Misses++
|
||||
c.metricsMu.Unlock()
|
||||
|
||||
if getOpts.loader != nil {
|
||||
return getOpts.loader.Load(c, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
c.metricsMu.Lock()
|
||||
c.metrics.Hits++
|
||||
c.metricsMu.Unlock()
|
||||
|
||||
return elem.Value.(*Item[K, V])
|
||||
return c.getWithOpts(key, true, opts...)
|
||||
}
|
||||
|
||||
// Delete deletes an item from the cache. If the item associated with
|
||||
@@ -286,12 +327,75 @@ func (c *Cache[K, V]) Delete(key K) {
|
||||
c.items.mu.Lock()
|
||||
defer c.items.mu.Unlock()
|
||||
|
||||
elem := c.items.values[key]
|
||||
if elem == nil {
|
||||
return
|
||||
c.delete(key)
|
||||
}
|
||||
|
||||
// Has checks whether the key exists in the cache.
|
||||
func (c *Cache[K, V]) Has(key K) bool {
|
||||
c.items.mu.RLock()
|
||||
defer c.items.mu.RUnlock()
|
||||
|
||||
_, ok := c.items.values[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
// GetOrSet retrieves an item from the cache by the provided key.
|
||||
// If the item is not found, it is created with the provided options and
|
||||
// then returned.
|
||||
// The bool return value is true if the item was found, false if created
|
||||
// during the execution of the method.
|
||||
// If the loader is non-nil (i.e., used as an option or specified when
|
||||
// creating the cache instance), its execution is skipped.
|
||||
func (c *Cache[K, V]) GetOrSet(key K, value V, opts ...Option[K, V]) (*Item[K, V], bool) {
|
||||
c.items.mu.Lock()
|
||||
defer c.items.mu.Unlock()
|
||||
|
||||
elem := c.getWithOpts(key, false, opts...)
|
||||
if elem != nil {
|
||||
return elem, true
|
||||
}
|
||||
|
||||
c.evict(EvictionReasonDeleted, elem)
|
||||
setOpts := options[K, V]{
|
||||
ttl: c.options.ttl,
|
||||
}
|
||||
applyOptions(&setOpts, opts...) // used only to update the TTL
|
||||
|
||||
item := c.set(key, value, setOpts.ttl)
|
||||
|
||||
return item, false
|
||||
}
|
||||
|
||||
// GetAndDelete retrieves an item from the cache by the provided key and
|
||||
// then deletes it.
|
||||
// The bool return value is true if the item was found before
|
||||
// its deletion, false if not.
|
||||
// If the loader is non-nil (i.e., used as an option or specified when
|
||||
// creating the cache instance), it is executed normaly, i.e., only when
|
||||
// the item is not found.
|
||||
func (c *Cache[K, V]) GetAndDelete(key K, opts ...Option[K, V]) (*Item[K, V], bool) {
|
||||
c.items.mu.Lock()
|
||||
|
||||
elem := c.getWithOpts(key, false, opts...)
|
||||
if elem == nil {
|
||||
c.items.mu.Unlock()
|
||||
|
||||
getOpts := options[K, V]{
|
||||
loader: c.options.loader,
|
||||
}
|
||||
applyOptions(&getOpts, opts...) // used only to update the loader
|
||||
|
||||
if getOpts.loader != nil {
|
||||
item := getOpts.loader.Load(c, key)
|
||||
return item, item != nil
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c.delete(key)
|
||||
c.items.mu.Unlock()
|
||||
|
||||
return elem, true
|
||||
}
|
||||
|
||||
// DeleteAll deletes all items from the cache.
|
||||
@@ -332,7 +436,7 @@ func (c *Cache[K, V]) Touch(key K) {
|
||||
c.items.mu.Unlock()
|
||||
}
|
||||
|
||||
// Len returns the number of items in the cache.
|
||||
// Len returns the total number of items in the cache.
|
||||
func (c *Cache[K, V]) Len() int {
|
||||
c.items.mu.RLock()
|
||||
defer c.items.mu.RUnlock()
|
||||
@@ -370,6 +474,24 @@ func (c *Cache[K, V]) Items() map[K]*Item[K, V] {
|
||||
return items
|
||||
}
|
||||
|
||||
// Range calls fn for each item present in the cache. If fn returns false,
|
||||
// Range stops the iteration.
|
||||
func (c *Cache[K, V]) Range(fn func(item *Item[K, V]) bool) {
|
||||
c.items.mu.RLock()
|
||||
for item := c.items.lru.Front(); item != c.items.lru.Back().Next(); item = item.Next() {
|
||||
i := item.Value.(*Item[K, V])
|
||||
c.items.mu.RUnlock()
|
||||
|
||||
if !fn(i) {
|
||||
return
|
||||
}
|
||||
|
||||
if item.Next() != nil {
|
||||
c.items.mu.RLock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Metrics returns the metrics of the cache.
|
||||
func (c *Cache[K, V]) Metrics() Metrics {
|
||||
c.metricsMu.RLock()
|
||||
@@ -378,8 +500,8 @@ func (c *Cache[K, V]) Metrics() Metrics {
|
||||
return c.metrics
|
||||
}
|
||||
|
||||
// Start starts an automatic cleanup process that
|
||||
// periodically deletes expired items.
|
||||
// Start starts an automatic cleanup process that periodically deletes
|
||||
// expired items.
|
||||
// It blocks until Stop is called.
|
||||
func (c *Cache[K, V]) Start() {
|
||||
waitDur := func() time.Duration {
|
||||
|
||||
33
vendor/github.com/jellydator/ttlcache/v3/item.go
generated
vendored
33
vendor/github.com/jellydator/ttlcache/v3/item.go
generated
vendored
@@ -9,8 +9,8 @@ const (
|
||||
// NoTTL indicates that an item should never expire.
|
||||
NoTTL time.Duration = -1
|
||||
|
||||
// DefaultTTL indicates that the default TTL
|
||||
// value should be used.
|
||||
// DefaultTTL indicates that the default TTL value of the cache
|
||||
// instance should be used.
|
||||
DefaultTTL time.Duration = 0
|
||||
)
|
||||
|
||||
@@ -21,8 +21,8 @@ type Item[K comparable, V any] struct {
|
||||
// - data fields are being read inside accessor methods
|
||||
// - data fields are being updated
|
||||
// when data fields are being read in one of the cache's
|
||||
// methods, we can be sure that these fields are not modified in
|
||||
// parallel since the item list is locked by its own mutex as
|
||||
// methods, we can be sure that these fields are not modified
|
||||
// concurrently since the item list is locked by its own mutex as
|
||||
// well, so locking this mutex would be redundant.
|
||||
// In other words, this mutex is only useful when these fields
|
||||
// are being read from the outside (e.g. in event functions).
|
||||
@@ -32,21 +32,27 @@ type Item[K comparable, V any] struct {
|
||||
ttl time.Duration
|
||||
expiresAt time.Time
|
||||
queueIndex int
|
||||
version int64
|
||||
}
|
||||
|
||||
// newItem creates a new cache item.
|
||||
func newItem[K comparable, V any](key K, value V, ttl time.Duration) *Item[K, V] {
|
||||
func newItem[K comparable, V any](key K, value V, ttl time.Duration, enableVersionTracking bool) *Item[K, V] {
|
||||
item := &Item[K, V]{
|
||||
key: key,
|
||||
value: value,
|
||||
ttl: ttl,
|
||||
}
|
||||
|
||||
if !enableVersionTracking {
|
||||
item.version = -1
|
||||
}
|
||||
|
||||
item.touch()
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
// update modifies the item's value and TTL.
|
||||
// update modifies the item's value, TTL, and version.
|
||||
func (item *Item[K, V]) update(value V, ttl time.Duration) {
|
||||
item.mu.Lock()
|
||||
defer item.mu.Unlock()
|
||||
@@ -58,6 +64,11 @@ func (item *Item[K, V]) update(value V, ttl time.Duration) {
|
||||
// 0 or below
|
||||
item.expiresAt = time.Time{}
|
||||
item.touchUnsafe()
|
||||
|
||||
// update version if enabled
|
||||
if item.version > -1 {
|
||||
item.version++
|
||||
}
|
||||
}
|
||||
|
||||
// touch updates the item's expiration timestamp.
|
||||
@@ -128,3 +139,13 @@ func (item *Item[K, V]) ExpiresAt() time.Time {
|
||||
|
||||
return item.expiresAt
|
||||
}
|
||||
|
||||
// Version returns the version of the item. It shows the total number of
|
||||
// changes made to the item.
|
||||
// If version tracking is disabled, the return value is always -1.
|
||||
func (item *Item[K, V]) Version() int64 {
|
||||
item.mu.RLock()
|
||||
defer item.mu.RUnlock()
|
||||
|
||||
return item.version
|
||||
}
|
||||
|
||||
3
vendor/github.com/jellydator/ttlcache/v3/metrics.go
generated
vendored
3
vendor/github.com/jellydator/ttlcache/v3/metrics.go
generated
vendored
@@ -12,7 +12,8 @@ type Metrics struct {
|
||||
Hits uint64
|
||||
|
||||
// Misses specifies how many items were not found in the cache.
|
||||
// Retrievals made with a loader function are tracked as well.
|
||||
// Retrievals made with a loader function are considered misses as
|
||||
// well.
|
||||
Misses uint64
|
||||
|
||||
// Evictions specifies how many items were removed from the
|
||||
|
||||
26
vendor/github.com/jellydator/ttlcache/v3/options.go
generated
vendored
26
vendor/github.com/jellydator/ttlcache/v3/options.go
generated
vendored
@@ -17,10 +17,11 @@ func (fn optionFunc[K, V]) apply(opts *options[K, V]) {
|
||||
|
||||
// options holds all available cache configuration options.
|
||||
type options[K comparable, V any] struct {
|
||||
capacity uint64
|
||||
ttl time.Duration
|
||||
loader Loader[K, V]
|
||||
disableTouchOnHit bool
|
||||
capacity uint64
|
||||
ttl time.Duration
|
||||
loader Loader[K, V]
|
||||
disableTouchOnHit bool
|
||||
enableVersionTracking bool
|
||||
}
|
||||
|
||||
// applyOptions applies the provided option values to the option struct.
|
||||
@@ -31,7 +32,7 @@ func applyOptions[K comparable, V any](v *options[K, V], opts ...Option[K, V]) {
|
||||
}
|
||||
|
||||
// WithCapacity sets the maximum capacity of the cache.
|
||||
// It has no effect when passing into Get().
|
||||
// It has no effect when used with Get().
|
||||
func WithCapacity[K comparable, V any](c uint64) Option[K, V] {
|
||||
return optionFunc[K, V](func(opts *options[K, V]) {
|
||||
opts.capacity = c
|
||||
@@ -39,15 +40,24 @@ func WithCapacity[K comparable, V any](c uint64) Option[K, V] {
|
||||
}
|
||||
|
||||
// WithTTL sets the TTL of the cache.
|
||||
// It has no effect when passing into Get().
|
||||
// It has no effect when used with Get().
|
||||
func WithTTL[K comparable, V any](ttl time.Duration) Option[K, V] {
|
||||
return optionFunc[K, V](func(opts *options[K, V]) {
|
||||
opts.ttl = ttl
|
||||
})
|
||||
}
|
||||
|
||||
// WithVersion activates item version tracking.
|
||||
// If version tracking is disabled, the version is always -1.
|
||||
// It has no effect when used with Get().
|
||||
func WithVersion[K comparable, V any](enable bool) Option[K, V] {
|
||||
return optionFunc[K, V](func(opts *options[K, V]) {
|
||||
opts.enableVersionTracking = enable
|
||||
})
|
||||
}
|
||||
|
||||
// WithLoader sets the loader of the cache.
|
||||
// When passing into Get(), it sets an epheral loader that
|
||||
// When passing into Get(), it sets an ephemeral loader that
|
||||
// is used instead of the cache's default one.
|
||||
func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V] {
|
||||
return optionFunc[K, V](func(opts *options[K, V]) {
|
||||
@@ -58,7 +68,7 @@ func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V] {
|
||||
// WithDisableTouchOnHit prevents the cache instance from
|
||||
// extending/touching an item's expiration timestamp when it is being
|
||||
// retrieved.
|
||||
// When passing into Get(), it overrides the default value of the
|
||||
// When used with Get(), it overrides the default value of the
|
||||
// cache.
|
||||
func WithDisableTouchOnHit[K comparable, V any]() Option[K, V] {
|
||||
return optionFunc[K, V](func(opts *options[K, V]) {
|
||||
|
||||
2
vendor/modules.txt
vendored
2
vendor/modules.txt
vendored
@@ -1163,7 +1163,7 @@ github.com/jbenet/go-context/io
|
||||
# github.com/jellydator/ttlcache/v2 v2.11.1
|
||||
## explicit; go 1.15
|
||||
github.com/jellydator/ttlcache/v2
|
||||
# github.com/jellydator/ttlcache/v3 v3.0.1
|
||||
# github.com/jellydator/ttlcache/v3 v3.1.0
|
||||
## explicit; go 1.18
|
||||
github.com/jellydator/ttlcache/v3
|
||||
# github.com/jmespath/go-jmespath v0.4.0
|
||||
|
||||
Reference in New Issue
Block a user