GithubHelp home page GithubHelp logo

go-pkgz / expirable-cache Goto Github PK

View Code? Open in Web Editor NEW
77.0 4.0 9.0 25 KB

Expirable Go LRC\LRU cache without goroutines

Home Page: https://go-pkgz.umputun.dev/expirable-cache/

License: MIT License

Go 100.00%
cache expirable-cache lru-cache lrc-cache go go-library

expirable-cache's People

Contributors

paskal avatar theshamuel avatar umputun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

expirable-cache's Issues

Feature request: Add option to get expiry time

I'd like to store the cache on disk when I close my application, but I need a way to get the expiry time for each item so I can store it.
I'd also need to set it back later, so having the option to set the ttl with time.Time would also be handy, but not as important since I can just subtract the current time.

Basic get does not work

The following code returns nil regardless of what key is passed in. The keys are obviously there and returned by the .Keys() function

Why can I not access the actual cache values?

package cache

import (
	"errors"
	"fmt"
	"log"
	"time"

	common "github.com/awilson9/redis_proxy/go/pkg/common"
	config "github.com/awilson9/redis_proxy/go/pkg/config"
	cache "github.com/go-pkgz/expirable-cache"
	lru "github.com/go-pkgz/expirable-cache"
)

type LRUCache struct {
	Cache  lru.Cache
	Expiry int
}

func (lruCache *LRUCache) InitCache() {
	cfg, err := config.NewConfig(common.ConfigPath)
	if err != nil {
		log.Fatal(err)
	}
	var capacity = cfg.App.Cache.Capacity
	var ttl = cfg.App.Cache.Expiry
	log.Printf("Initializing LRU cache with capacity %v expiration %v", capacity, ttl)
	cache, err := lru.NewCache(cache.MaxKeys(capacity), cache.TTL(time.Duration(ttl)*time.Millisecond))
	if err != nil {
		log.Fatal(err)
	}
	lruCache.Expiry = ttl
	lruCache.Cache = cache
}

func (lruCache *LRUCache) GetFromCache(lruCacheKey string) (value string, ok bool) {
	interfaceValue, ok := lruCache.Cache.Get(lruCacheKey)
	var valueStr string = ""
	if interfaceValue != nil {
		valueStr = interfaceValue.(string)
	}

	return valueStr, ok
}

func (lruCache *LRUCache) AddToCache(lruCacheKey string, lruCacheValue string) {
	lruCache.Cache.Set(lruCacheKey, lruCacheValue, 0)
}

func (lruCache *LRUCache) DumpCache() ([]common.CacheEntry, error) {
	keys := lruCache.Cache.Keys()
	cacheValues := make([]common.CacheEntry, len(keys))
	log.Printf("%v", keys)
	for _, key := range keys {
		log.Printf("key: %s", key)
		value, ok := lruCache.GetFromCache(key)
		log.Printf("value: %v", value)
		if ok {
			cacheValues = append(cacheValues, common.CacheEntry{Key: key, Value: fmt.Sprintf("%v", value)})
		} else {
			return nil, errors.New("Unable to dump cache")
		}

	}
	return cacheValues, nil
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.