Files
mattermost/store/redis_supplier.go
Christopher Speller 686c2fbab7 Structured logging (#8673)
* Implementing structured logging

* Changes to en.json to allow refactor to run.

* Fixing global logger

* Structured logger initalization.

* Add caller.

* Do some log redirection.

* Auto refactor

* Cleaning up l4g reference and removing dependancy.

* Removing junk.

* Copyright headers.

* Fixing tests

* Revert "Changes to en.json to allow refactor to run."

This reverts commit fd8249e99b.

* Fixing some auto refactor strangeness and typo.

* Making keys more human readable.
2018-04-27 12:49:45 -07:00

88 lines
1.8 KiB
Go

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package store
import (
"bytes"
"encoding/gob"
"time"
"github.com/go-redis/redis"
"github.com/mattermost/mattermost-server/mlog"
)
const REDIS_EXPIRY_TIME = 30 * time.Minute
type RedisSupplier struct {
next LayeredStoreSupplier
client *redis.Client
}
func GetBytes(key interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(key)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func DecodeBytes(input []byte, thing interface{}) error {
dec := gob.NewDecoder(bytes.NewReader(input))
return dec.Decode(thing)
}
func NewRedisSupplier() *RedisSupplier {
supplier := &RedisSupplier{}
supplier.client = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
if _, err := supplier.client.Ping().Result(); err != nil {
mlog.Error("Unable to ping redis server: " + err.Error())
return nil
}
return supplier
}
func (s *RedisSupplier) save(key string, value interface{}, expiry time.Duration) error {
if bytes, err := GetBytes(value); err != nil {
return err
} else {
if err := s.client.Set(key, bytes, expiry).Err(); err != nil {
return err
}
}
return nil
}
func (s *RedisSupplier) load(key string, writeTo interface{}) (bool, error) {
if data, err := s.client.Get(key).Bytes(); err != nil {
if err == redis.Nil {
return false, nil
} else {
return false, err
}
} else {
if err := DecodeBytes(data, writeTo); err != nil {
return false, err
}
}
return true, nil
}
func (s *RedisSupplier) SetChainNext(next LayeredStoreSupplier) {
s.next = next
}
func (s *RedisSupplier) Next() LayeredStoreSupplier {
return s.next
}