Skip to content

Commit

Permalink
Pass Starlark thread pointer to cache operations (#57)
Browse files Browse the repository at this point in the history
This allows cache implementations to be aware of threads and act
differently or even use different caches based on context.
  • Loading branch information
rohansingh authored Jul 1, 2021
1 parent 13a8951 commit 588da49
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions runtime/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
const DefaultExpirationSeconds = 60

type Cache interface {
Set(key string, value []byte, ttl int64) error
Get(key string) ([]byte, bool, error)
Set(thread *starlark.Thread, key string, value []byte, ttl int64) error
Get(thread *starlark.Thread, key string) ([]byte, bool, error)
}

type InMemoryCacheRecord struct {
Expand All @@ -31,7 +31,7 @@ func NewInMemoryCache() *InMemoryCache {
return &InMemoryCache{records: map[string]*InMemoryCacheRecord{}}
}

func (c *InMemoryCache) Get(key string) (value []byte, found bool, err error) {
func (c *InMemoryCache) Get(_ *starlark.Thread, key string) (value []byte, found bool, err error) {
c.mutex.RLock()
defer c.mutex.RUnlock()

Expand All @@ -48,7 +48,7 @@ func (c *InMemoryCache) Get(key string) (value []byte, found bool, err error) {
return r.data, true, nil
}

func (c *InMemoryCache) Set(key string, value []byte, ttl int64) error {
func (c *InMemoryCache) Set(_ *starlark.Thread, key string, value []byte, ttl int64) error {
c.mutex.Lock()
defer c.mutex.Unlock()

Expand Down Expand Up @@ -108,7 +108,7 @@ func cacheGet(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple,
return starlark.None, nil
}

val, found, err := cache.Get(cacheKey)
val, found, err := cache.Get(thread, cacheKey)

if err != nil {
// don't fail just because cache is misbehaving
Expand Down Expand Up @@ -160,7 +160,7 @@ func cacheSet(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple,
return starlark.None, nil
}

err := cache.Set(cacheKey, []byte(val.GoString()), ttl64)
err := cache.Set(thread, cacheKey, []byte(val.GoString()), ttl64)
if err != nil {
log.Printf("setting %s in cache: %v", cacheKey, err)
}
Expand Down

0 comments on commit 588da49

Please sign in to comment.