Skip to content

Commit

Permalink
Release v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sezaakgun committed Oct 21, 2022
2 parents 390cc6c + f45756a commit 822a490
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Empty file added .github/workflows/cxflow.yml
Empty file.
20 changes: 20 additions & 0 deletions inscacheable/cacheable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@ import (
type Cacher[K comparable, V any] interface {
Get(k K) V
Set(k K, v V, ttl time.Duration)
Exists(k K) bool
Delete(k K)
}

type Cache[K comparable, V any] struct {
cache *ttlcache.Cache[K, V]
}

// Get returns a value at the given key.
// It is non-null safe method, so be sure to use Exists before getting the value.
func (c *Cache[K, V]) Get(k K) V {
return c.cache.Get(k).Value()
}

// Set stores the value at the given key.
// It accepts ttl=0 that indicates the default TTL given at initialization should be used.
func (c *Cache[K, V]) Set(k K, v V, ttl time.Duration) {
c.cache.Set(k, v, ttl)
}

// Exists checks if key is set in the cache.
func (c *Cache[K, V]) Exists(k K) bool {
return c.cache.Get(k) != nil
}

// Delete deletes the key from the cache.
func (c *Cache[K, V]) Delete(k K) {
c.cache.Delete(k)
}

// Cacheable is the main function that should be used as
// func getter(key string) string { ... the original getter function ... }
// var ttl = 1 * time.Minute
Expand Down Expand Up @@ -54,6 +70,10 @@ func makeCache[K comparable, V any](ttl *time.Duration, loader ttlcache.LoaderFu
}

func makeLoader[K comparable, V any](getter func(key K) V) ttlcache.LoaderFunc[K, V] {
if getter == nil {
return nil
}

fn :=
func(c *ttlcache.Cache[K, V], key K) *ttlcache.Item[K, V] {
var v = getter(key)
Expand Down

0 comments on commit 822a490

Please sign in to comment.