Skip to content
Snippets Groups Projects
Select Git revision
  • 2850aeed2c8ffcf6a061f2bb36c1b18cda41f9a1
  • master default
  • method_check
  • custom_prefix
  • package
  • cookies
  • v2.1.1
  • v2.1.0
  • v2.1.0-rc5
  • v2.1.0-rc4
  • v2.1.0-rc3
  • v2.1.0-rc2
  • v2.1.0-rc1
  • v2.0.7
  • v2.0.6
  • v2.0.5
  • v2.0.4
  • v2.0.3
  • v2.0.2
  • v2.0.1
  • v2.0.0
  • v1.2.8
  • v1.2.7
  • v1.2.6
  • v1.2.5
  • v1.2.4
26 results

user_context.go

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,
    	}
    }