Skip to content

Commit

Permalink
expose Item
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleSanderson authored Nov 24, 2024
1 parent 6933dc0 commit b4c88b6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ type timeentry struct {
var db *bolt.DB
var clientmap = ttlcache.New[qbittorrent.Config, *qbittorrent.Client](
ttlcache.Options[qbittorrent.Config, *qbittorrent.Client]{}.
SetDefaultTTL(time.Minute * 5))
SetDefaultTTL(time.Minute * 5).
SetTimerResolution(time.Minute * 1))

var torrentmap = ttlcache.New[qbittorrent.Config, *timeentry](
ttlcache.Options[qbittorrent.Config, *timeentry]{}.
Expand All @@ -90,7 +91,8 @@ var torrentmap = ttlcache.New[qbittorrent.Config, *timeentry](

var titlemap = ttlcache.New[string, *rls.Release](
ttlcache.Options[string, *rls.Release]{}.
SetDefaultTTL(time.Minute * 15))
SetDefaultTTL(time.Minute * 15).
SetTimerResolution(time.Minute * 5))

func main() {
initDatabase()
Expand Down
4 changes: 2 additions & 2 deletions pkg/ttlcache/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Cache[K comparable, V any] struct {
l sync.RWMutex
o Options[K, V]
ch chan time.Time
m map[K]item[V]
m map[K]Item[V]
}

type item[V any] struct {
type Item[V any] struct {
t time.Time
d time.Duration
v V
Expand Down
38 changes: 34 additions & 4 deletions pkg/ttlcache/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ttlcache

import "time"

func (c *Cache[K, V]) get(key K) (item[V], bool) {
func (c *Cache[K, V]) get(key K) (Item[V], bool) {
c.l.RLock()
defer c.l.RUnlock()
v, ok := c.m[key]
Expand All @@ -14,7 +14,7 @@ func (c *Cache[K, V]) get(key K) (item[V], bool) {
return v, ok
}

func (c *Cache[K, V]) set(key K, it item[V]) {
func (c *Cache[K, V]) set(key K, it Item[V]) {
it.t = c.getDuration(it.d)

c.l.Lock()
Expand All @@ -24,7 +24,7 @@ func (c *Cache[K, V]) set(key K, it item[V]) {
}

func (c *Cache[K, V]) delete(key K, reason DeallocationReason) {
var v item[V]
var v Item[V]
c.l.Lock()
defer c.l.Unlock()

Expand All @@ -39,14 +39,32 @@ func (c *Cache[K, V]) delete(key K, reason DeallocationReason) {
c.deleteUnsafe(key, v, reason)
}

func (c *Cache[K, V]) deleteUnsafe(key K, v item[V], reason DeallocationReason) {
func (c *Cache[K, V]) deleteUnsafe(key K, v Item[V], reason DeallocationReason) {
delete(c.m, key)

if c.o.deallocationFunc != nil {
c.o.deallocationFunc(key, v.v, reason)
}
}

func (c *Cache[K, V]) getkeys() []K {
c.l.RLock()
defer c.l.RUnlock()

keys := make([]K, len(c.m))
for k := range c.m {
keys = append(keys, k)
}

return keys
}

func (c *Cache[K, V]) close() {
c.l.Lock()
defer c.l.Unlock()
close(c.ch)
}

func (c *Cache[K, V]) getDuration(d time.Duration) time.Time {
switch d {
case NoTTL:
Expand All @@ -58,3 +76,15 @@ func (c *Cache[K, V]) getDuration(d time.Duration) time.Time {

return time.Time{}
}

func (i *Item[V]) getDuration() time.Duration {
return i.d
}

func (i *Item[V]) getTime() time.Time {
return i.t
}

func (i *Item[V]) getValue() V {
return i.v
}
41 changes: 28 additions & 13 deletions pkg/ttlcache/ttlcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func New[K comparable, V any](options Options[K, V]) *Cache[K, V] {
c := Cache[K, V]{
o: options,
ch: make(chan time.Time, 1000),
m: make(map[K]item[V]),
m: make(map[K]Item[V]),
}

if options.defaultTTL != NoTTL && options.defaultResolution == 0 {
Expand All @@ -36,35 +36,50 @@ func (c *Cache[K, V]) Get(key K) (V, bool) {
return it.v, ok
}

func (c *Cache[K, V]) GetItem(key K) (Item[V], bool) {
it, ok := c.get(key)
if !ok {
return it, ok
}

if !c.o.noUpdateTime && !it.t.IsZero() && c.getDuration(it.d).After(it.t) {
c.set(key, it)
}

return it, ok
}

func (c *Cache[K, V]) Set(key K, value V, duration time.Duration) bool {
if c.o.defaultTTL == NoTTL && duration == DefaultTTL {
duration = NoTTL
}

c.set(key, item[V]{v: value, d: duration})
c.set(key, Item[V]{v: value, d: duration})
return true
}

func (c *Cache[K, V]) Delete(key K) {
c.delete(key, ReasonDeleted)
}

func (c *Cache[K, V]) GetKeys() []K {
return c.getkeys()
}

func (c *Cache[K, V]) Close() {
c.l.Lock()
defer c.l.Unlock()
close(c.ch)
c.close()
}

func (c *Cache[K, V]) GetKeys() []K {
c.l.RLock()
defer c.l.RUnlock()
func (i *Item[V]) GetDuration() time.Duration {
return i.getDuration()
}

ret := make([]K, 0, len(c.m))
for k := range c.m {
ret = append(ret, k)
}
func (i *Item[V]) GetTime() time.Time {
return i.getTime()
}

return ret
func (i *Item[V]) GetValue() V {
return i.getValue()
}

func (o Options[K, V]) SetTimerResolution(d time.Duration) Options[K, V] {
Expand Down

0 comments on commit b4c88b6

Please sign in to comment.