Skip to content
Snippets Groups Projects
Select Git revision
  • c28c69bfe6e3c16b3e4fb95ef4f80c508e88adaa
  • main default protected
  • timelens
3 results

lib.rs

Blame
  • cache_redis.go 988 B
    package main
    
    import (
    	"encoding/json"
    	"errors"
    	"git.kuschku.de/justjanne/bahn-api"
    	"github.com/go-redis/cache"
    	"github.com/go-redis/redis"
    	"time"
    )
    
    type RedisCache struct {
    	backend        *cache.Codec
    	expirationTime time.Duration
    }
    
    func (m RedisCache) Set(key string, value interface{}) error {
    	return m.backend.Set(&cache.Item{
    		Key:        key,
    		Object:     value,
    		Expiration: m.expirationTime,
    	})
    }
    
    func (m RedisCache) Get(key string, value interface{}) error {
    	err := m.backend.Get(key, &value)
    	if err != nil {
    		return err
    	} else if value == nil {
    		return errors.New("redis returned empty result")
    	}
    	return nil
    }
    
    func NewRedisCache(address string, password string, expirationTime time.Duration) bahn.CacheBackend {
    	return RedisCache{
    		backend: &cache.Codec{
    			Redis: redis.NewClient(&redis.Options{
    				Addr:     address,
    				Password: password,
    			}),
    			Marshal:   json.Marshal,
    			Unmarshal: json.Unmarshal,
    		},
    		expirationTime: expirationTime,
    	}
    }