| 1 | // Copyright (c) 2025 Alexey Mayshev and contributors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package otterscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ |
| 16 | |
| 17 | import ( |
| 18 | "timescip-go gomod github.com/golang/go/src go1.24.0 time/" |
| 19 | ) |
| 20 | |
| 21 | // ExpiryCalculator calculates when cache entries expire. A single expiration time is retained so that the lifetime |
| 22 | // of an entry may be extended or reduced by subsequent evaluations. |
| 23 | type ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K comparable, V any] interface { |
| 24 | // ExpireAfterCreate specifies that the entry should be automatically removed from the cache once the duration has |
| 25 | // elapsed after the entry's creation. To indicate no expiration, an entry may be given an |
| 26 | // excessively long period. |
| 27 | // |
| 28 | // NOTE: ExpiresAtNano and RefreshableAtNano are not initialized at this stage. |
| 29 | ExpireAfterCreatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#ExpireAfterCreate.(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 30 | // ExpireAfterUpdate specifies that the entry should be automatically removed from the cache once the duration has |
| 31 | // elapsed after the replacement of its value. To indicate no expiration, an entry may be given an |
| 32 | // excessively long period. The entry.ExpiresAfter() may be returned to not modify the expiration time. |
| 33 | ExpireAfterUpdatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#ExpireAfterUpdate.(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V], oldValue V) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 34 | // ExpireAfterRead specifies that the entry should be automatically removed from the cache once the duration has |
| 35 | // elapsed after its last read. To indicate no expiration, an entry may be given an excessively |
| 36 | // long period. The entry.ExpiresAfter() may be returned to not modify the expiration time. |
| 37 | ExpireAfterReadscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#ExpireAfterRead.(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 38 | } |
| 39 | |
| 40 | type varExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#[K comparable, V any] struct { |
| 41 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#f. func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 42 | } |
| 43 | |
| 44 | func (c *varExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#[K, V]) ExpireAfterCreatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#ExpireAfterCreate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 45 | return c.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#f.(entry) |
| 46 | } |
| 47 | |
| 48 | func (c *varExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#[K, V]) ExpireAfterUpdatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#ExpireAfterUpdate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V], oldValue V) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 49 | return entry.ExpiresAfterscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#ExpiresAfter().() |
| 50 | } |
| 51 | |
| 52 | func (c *varExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#[K, V]) ExpireAfterReadscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#ExpireAfterRead().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 53 | return entry.ExpiresAfterscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#ExpiresAfter().() |
| 54 | } |
| 55 | |
| 56 | // ExpiryCreating returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 57 | // the cache once the duration has elapsed after the entry's creation. The expiration time is |
| 58 | // not modified when the entry is updated or read. |
| 59 | func ExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCreating().[K comparable, V any](duration timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 60 | return ExpiryCreatingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCreatingFunc().(func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 61 | return duration |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | // ExpiryCreatingFunc returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 66 | // the cache once the duration has elapsed after the entry's creation. The expiration time is |
| 67 | // not modified when the entry is updated or read. |
| 68 | func ExpiryCreatingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCreatingFunc().[K comparable, V any](f func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 69 | return &varExpiryCreatingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#[K, V]{ |
| 70 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryCreating#f.: f, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | type varExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#[K comparable, V any] struct { |
| 75 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#f. func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 76 | } |
| 77 | |
| 78 | func (w *varExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#[K, V]) ExpireAfterCreatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#ExpireAfterCreate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 79 | return w.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#f.(entry) |
| 80 | } |
| 81 | |
| 82 | func (w *varExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#[K, V]) ExpireAfterUpdatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#ExpireAfterUpdate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V], oldValue V) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 83 | return w.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#f.(entry) |
| 84 | } |
| 85 | |
| 86 | func (w *varExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#[K, V]) ExpireAfterReadscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#ExpireAfterRead().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 87 | return entry.ExpiresAfterscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#ExpiresAfter().() |
| 88 | } |
| 89 | |
| 90 | // ExpiryWriting returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 91 | // the cache once the duration has elapsed after the entry's creation or replacement of its value. |
| 92 | // The expiration time is not modified when the entry is read. |
| 93 | func ExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryWriting().[K comparable, V any](duration timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 94 | return ExpiryWritingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryWritingFunc().(func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 95 | return duration |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | // ExpiryWritingFunc returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 100 | // the cache once the duration has elapsed after the entry's creation or replacement of its value. |
| 101 | // The expiration time is not modified when the entry is read. |
| 102 | func ExpiryWritingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryWritingFunc().[K comparable, V any](f func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 103 | return &varExpiryWritingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#[K, V]{ |
| 104 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryWriting#f.: f, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | type varExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#[K comparable, V any] struct { |
| 109 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#f. func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# |
| 110 | } |
| 111 | |
| 112 | func (a *varExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#[K, V]) ExpireAfterCreatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#ExpireAfterCreate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 113 | return a.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#f.(entry) |
| 114 | } |
| 115 | |
| 116 | func (a *varExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#[K, V]) ExpireAfterUpdatescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#ExpireAfterUpdate().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V], oldValue V) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 117 | return a.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#f.(entry) |
| 118 | } |
| 119 | |
| 120 | func (a *varExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#[K, V]) ExpireAfterReadscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#ExpireAfterRead().(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 121 | return a.fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#f.(entry) |
| 122 | } |
| 123 | |
| 124 | // ExpiryAccessing returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 125 | // the cache once the duration has elapsed after the entry's creation, replacement of its value, |
| 126 | // or after it was last read. |
| 127 | func ExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryAccessing().[K comparable, V any](duration timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 128 | return ExpiryAccessingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryAccessingFunc().(func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration# { |
| 129 | return duration |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | // ExpiryAccessingFunc returns an [ExpiryCalculator] that specifies that the entry should be automatically deleted from |
| 134 | // the cache once the duration has elapsed after the entry's creation, replacement of its value, |
| 135 | // or after it was last read. |
| 136 | func ExpiryAccessingFuncscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryAccessingFunc().[K comparable, V any](f func(entry Entryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/Entry#[K, V]) timescip-go gomod github.com/golang/go/src go1.24.0 time/.Durationscip-go gomod github.com/golang/go/src go1.24.0 time/Duration#) ExpiryCalculatorscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/ExpiryCalculator#[K, V] { |
| 137 | return &varExpiryAccessingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#[K, V]{ |
| 138 | fscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2`/varExpiryAccessing#f.: f, |
| 139 | } |
| 140 | } |