mirror of
				https://github.com/mickael-kerjean/filestash.git
				synced 2025-11-01 02:43:35 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			75 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package common
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"github.com/mitchellh/hashstructure"
 | |
| 	"github.com/patrickmn/go-cache"
 | |
| 	"time"
 | |
| )
 | |
| 
 | |
| type AppCache struct {
 | |
| 	Cache *cache.Cache
 | |
| }
 | |
| 
 | |
| func (a *AppCache) Get(key interface{}) interface{} {
 | |
| 	hash, err := hashstructure.Hash(key, nil)
 | |
| 	if err != nil {
 | |
| 		return nil
 | |
| 	}
 | |
| 	value, found := a.Cache.Get(fmt.Sprint(hash))
 | |
| 	if found == false {
 | |
| 		return nil
 | |
| 	}
 | |
| 	return value
 | |
| }
 | |
| 
 | |
| func (a *AppCache) Set(key map[string]string, value interface{}) {
 | |
| 	hash, err := hashstructure.Hash(key, nil)
 | |
| 	if err != nil {
 | |
| 		return
 | |
| 	}
 | |
| 	a.Cache.Set(fmt.Sprint(hash), value, cache.DefaultExpiration)
 | |
| }
 | |
| 
 | |
| func (a *AppCache) OnEvict(fn func(string, interface{})) {
 | |
| 	a.Cache.OnEvicted(fn)
 | |
| }
 | |
| 
 | |
| func NewAppCache(arg ...time.Duration) AppCache {
 | |
| 	var retention time.Duration = 5
 | |
| 	var cleanup time.Duration = 10
 | |
| 	if len(arg) > 0 {
 | |
| 		retention = arg[0]
 | |
| 		if len(arg) > 1 {
 | |
| 			cleanup = arg[1]
 | |
| 		}
 | |
| 	}
 | |
| 	c := AppCache{}
 | |
| 	c.Cache = cache.New(retention*time.Minute, cleanup*time.Minute)
 | |
| 	return c
 | |
| }
 | |
| 
 | |
| 
 | |
| // ============================================================================
 | |
| 
 | |
| 
 | |
| type KeyValueStore struct {
 | |
| 	cache map[string]interface{}
 | |
| }
 | |
| 
 | |
| func NewKeyValueStore() KeyValueStore {
 | |
| 	return KeyValueStore{ cache: make(map[string]interface{}) }
 | |
| }
 | |
| 
 | |
| func (this KeyValueStore) Get(key string) interface{} {
 | |
| 	return this.cache[key]
 | |
| }
 | |
| 
 | |
| func (this *KeyValueStore) Set(key string, value interface{}) {
 | |
| 	this.cache[key] = value
 | |
| }
 | |
| 
 | |
| func (this *KeyValueStore) Clear() {
 | |
| 	this.cache = make(map[string]interface{})
 | |
| }
 | 
