-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.go
169 lines (140 loc) · 3.53 KB
/
cache.go
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2020 iquota Authors. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package iquota
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/gomodule/redigo/redis"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
ErrNotFound = errors.New("not found")
)
type Quota struct {
Path string `json:"path"`
GracePeriod string `json:"pretty_grace_period"`
HardLimit int `json:"hard_limit"`
SoftLimit int `json:"soft_limit"`
Used int `json:"used"`
HardLimitInodes int `json:"hard_limit_inodes"`
SoftLimitInodes int `json:"soft_limit_inodes"`
UsedInodes int `json:"used_inodes"`
}
type Cache struct {
Expire int
}
func redisDial() (redis.Conn, error) {
conn, err := redis.Dial("tcp", viper.GetString("redis"))
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("Failed connecting to redis server")
return nil, err
}
return conn, err
}
func (c *Cache) redisFind(pattern string) ([]*Quota, error) {
conn, err := redisDial()
if err != nil {
return nil, err
}
defer conn.Close()
if strings.HasPrefix(pattern, "grp-") {
pattern = strings.TrimPrefix(pattern, "grp-")
}
keys, err := redis.Strings(conn.Do("KEYS", fmt.Sprintf("*%s", pattern)))
if err != nil {
if errors.Is(err, redis.ErrNil) {
return nil, ErrNotFound
}
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"keys": keys,
}).Error("Failed to find keys")
return nil, err
}
var quotas []*Quota
for _, key := range keys {
if len(pattern) > 0 && strings.HasPrefix(key, viper.GetString("home_dir")) {
continue
}
quota, err := c.unmarshalQuota(conn, key)
if err != nil {
continue
}
quotas = append(quotas, quota)
}
return quotas, nil
}
func (c *Cache) unmarshalQuota(conn redis.Conn, key string) (*Quota, error) {
rawJson, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
if errors.Is(err, redis.ErrNil) {
return nil, ErrNotFound
}
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"key": key,
}).Error("Failed to fetch quota from cache")
return nil, err
}
quota := &Quota{}
err = json.Unmarshal([]byte(rawJson), quota)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"key": key,
}).Error("Failed to Unmarshal quota")
return nil, err
}
return quota, nil
}
func (c *Cache) redisGet(key string) (*Quota, error) {
conn, err := redisDial()
if err != nil {
return nil, err
}
defer conn.Close()
return c.unmarshalQuota(conn, key)
}
func (c *Cache) redisSet(key string, iq *Quota) error {
conn, err := redisDial()
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
}).Error("Failed connecting to redis server")
return err
}
defer conn.Close()
out, err := json.Marshal(iq)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"key": key,
}).Error("Failed marshal quota response")
return err
}
_, err = conn.Do("SETEX", key, c.Expire, out)
if err != nil {
logrus.WithFields(logrus.Fields{
"err": err.Error(),
"key": key,
}).Error("Failed to set cache")
return err
}
return nil
}
func (c *Cache) SetDirectoryQuotaCache(path string, iq *Quota) error {
return c.redisSet(path, iq)
}
func (c *Cache) GetDirectoryQuotaCache(path string) (*Quota, error) {
c.redisFind("grp-ezurek")
return c.redisGet(path)
}
func (c *Cache) SearchDirectoryQuotaCache(pattern string) ([]*Quota, error) {
return c.redisFind(pattern)
}