summaryrefslogtreecommitdiffstats
path: root/store/redis_supplier.go
blob: ce8cb0f0d9e048e1a07476b0648face98c0b89c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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
}