Skip to content

Commit 7c0c7b3

Browse files
committed
[Queue] (*) Refactoring
1 parent 56ecd2b commit 7c0c7b3

File tree

4 files changed

+99
-93
lines changed

4 files changed

+99
-93
lines changed

bigcache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (bc *BigCacheClient) Set(key string, value interface{}, expire time.Duratio
5858
return bc.Client.Set(key, *b)
5959
}
6060

61-
// Get function will get value based on the key provided
61+
// GetByKey function will get value based on the key provided
6262
func (bc *BigCacheClient) Get(key string) (interface{}, error) {
6363
return bc.Client.Get(key)
6464
}

minimalism.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewMinimalism(config *Config) ICaching {
3636

3737
if item.expires < now {
3838
k, _ := key.(string)
39-
currentSession.items.Pop(k)
39+
currentSession.items.Dequeue(k)
4040
}
4141

4242
return true
@@ -70,15 +70,15 @@ func (ml *MinimalismClient) Middleware(hash hash.IHash) echo.MiddlewareFunc {
7070
}
7171
}
7272

73-
// Get ...
73+
// GetByKey ...
7474
func (ml *MinimalismClient) Get(key string) (interface{}, error) {
75-
obj, ok := ml.items.Get(key)
76-
if !ok {
77-
return "", errors.New("item with that key does not exist")
75+
obj, err := ml.items.GetByKey(key)
76+
if err != nil {
77+
return nil, errors.New("item with that key does not exist")
7878
}
7979

80-
item, err := obj.(MinimalismItem)
81-
if !err {
80+
item, ok := obj.(MinimalismItem)
81+
if !ok {
8282
return nil, errors.New("can not map object to MinimalismItem model")
8383
}
8484

@@ -95,13 +95,13 @@ func (ml *MinimalismClient) GetMany(keys []string) (map[string]interface{}, []st
9595
var itemNotFound []string
9696

9797
for _, key := range keys {
98-
obj, ok := ml.items.Get(key)
99-
if !ok {
98+
obj, err := ml.items.GetByKey(key)
99+
if err != nil {
100100
itemNotFound = append(itemNotFound, key)
101101
}
102102

103-
item, err := obj.(MinimalismItem)
104-
if !err {
103+
item, ok := obj.(MinimalismItem)
104+
if !ok {
105105
return nil, nil, errors.New("can not map object to MinimalismItem model")
106106
}
107107

@@ -116,13 +116,13 @@ func (ml *MinimalismClient) GetManyStrings(keys []string) (map[string]string, []
116116
var itemNotFound []string
117117

118118
for _, key := range keys {
119-
obj, ok := ml.items.Get(key)
120-
if !ok {
119+
obj, err := ml.items.GetByKey(key)
120+
if err != nil {
121121
itemNotFound = append(itemNotFound, key)
122122
}
123123

124-
item, err := obj.(MinimalismItem)
125-
if !err {
124+
item, ok := obj.(MinimalismItem)
125+
if !ok {
126126
return nil, nil, errors.New("can not map object to MinimalismItem model")
127127
}
128128

@@ -140,10 +140,12 @@ func (ml *MinimalismClient) Set(key string, value interface{}, expire time.Durat
140140
expires = time.Now().Add(expire).UnixNano()
141141
}
142142

143-
ml.items.Push(key, MinimalismItem{
143+
if err := ml.items.Enqueue(key, MinimalismItem{
144144
data: value,
145145
expires: expires,
146-
})
146+
}); err != nil {
147+
return err
148+
}
147149

148150
return nil
149151
}
@@ -167,7 +169,7 @@ func (ml *MinimalismClient) Range(f func(key, value interface{}) bool) {
167169

168170
// Delete deletes the key and its value from the cache.
169171
func (ml *MinimalismClient) Delete(key string) error {
170-
ml.items.Pop(key)
172+
ml.items.Dequeue(key)
171173

172174
return nil
173175
}

queue.go

+77-73
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,125 @@
11
package caching
22

33
import (
4+
"errors"
45
"reflect"
56
"sync"
67
)
78

8-
// Item ...
9-
type Item interface{}
10-
119
// Queue ...
1210
type Queue struct {
13-
items sync.Map
14-
keys []string
15-
maxSize int64
16-
curSize int64
17-
lock sync.RWMutex
11+
items sync.Map
12+
keys []string
13+
queueSize int64
14+
queueCurrentSize int64
15+
rwMutex sync.RWMutex
1816
}
1917

2018
// NewQueue ...
21-
func NewQueue(maxSize int64) *Queue {
19+
func NewQueue(queueSize int64) *Queue {
2220
currentQueue := Queue{
23-
items: sync.Map{},
24-
keys: []string{},
25-
maxSize: maxSize,
26-
curSize: 0,
27-
lock: sync.RWMutex{},
21+
items: sync.Map{},
22+
keys: []string{},
23+
queueSize: queueSize,
24+
queueCurrentSize: 0,
25+
rwMutex: sync.RWMutex{},
2826
}
2927

3028
return &currentQueue
3129
}
3230

33-
// Push ...
34-
func (q *Queue) Push(k string, t Item) bool {
35-
itemSize := int64(reflect.Type.Size(t))
36-
if itemSize > q.maxSize || k == "" {
37-
return false
31+
// Enqueue ...
32+
func (q *Queue) Enqueue(key string, value interface{}) error {
33+
itemSize := int64(reflect.Type.Size(value))
34+
if itemSize > q.queueSize || key == "" {
35+
return errors.New("key is empty or queue not enough space")
3836
}
3937

40-
// Clean room for new items
41-
for q.curSize+itemSize > q.maxSize {
42-
q.Pop("")
38+
// Clean space for new item
39+
for q.queueCurrentSize+itemSize > q.queueSize {
40+
q.Dequeue("")
4341
}
4442

45-
q.lock.Lock()
46-
q.curSize = q.curSize + int64(reflect.Type.Size(t))
47-
q.keys = append(q.keys, k)
48-
q.items.LoadOrStore(k, t)
49-
q.lock.Unlock()
50-
return true
43+
q.rwMutex.Lock()
44+
q.queueCurrentSize = q.queueCurrentSize + int64(reflect.Type.Size(value))
45+
q.keys = append(q.keys, key)
46+
q.items.LoadOrStore(key, value)
47+
q.rwMutex.Unlock()
48+
49+
return nil
5150
}
5251

53-
// Pop ...
54-
func (q *Queue) Pop(k string) *Item {
52+
// Dequeue ...
53+
func (q *Queue) Dequeue(key string) error {
5554
if q.IsEmpty() {
56-
return nil
55+
return errors.New("the queue is empty")
5756
}
5857

59-
var item interface{}
60-
var exits bool
61-
var idx int
58+
var (
59+
item interface{}
60+
exits bool
61+
keyIndex int
62+
)
6263

63-
if k != "" {
64-
// Remove item by key
65-
item, exits = q.items.Load(k)
66-
for i, n := range q.keys {
67-
if k == n {
68-
idx = i
64+
if key != "" {
65+
// Load item by key
66+
item, exits = q.items.Load(key)
67+
for i, k := range q.keys {
68+
if key == k {
69+
keyIndex = i
6970
break
7071
}
7172
}
7273
} else {
73-
// Remove first item
74-
key := q.keys[0]
75-
item, exits = q.items.Load(key)
76-
idx = 0
77-
if !exits {
78-
return nil
79-
}
74+
// Load the first item
75+
item, exits = q.items.Load(q.keys[0])
76+
keyIndex = 0
8077
}
8178

82-
q.lock.Lock()
83-
q.curSize = q.curSize - int64(reflect.Type.Size(item))
84-
q.items.Delete(k)
85-
q.keys = append(q.keys[:idx], q.keys[idx+1:]...)
86-
q.lock.Unlock()
87-
i, _ := (item).(Item)
79+
if !exits {
80+
return errors.New("this key does not exits")
81+
}
8882

89-
return &i
83+
q.rwMutex.Lock()
84+
q.queueCurrentSize = q.queueCurrentSize - int64(reflect.Type.Size(item))
85+
q.items.Delete(key)
86+
q.keys = append(q.keys[:keyIndex], q.keys[keyIndex+1:]...) //Update keys slice after remove this key from items map
87+
q.rwMutex.Unlock()
88+
89+
return nil
9090
}
9191

9292
// Get ...
93-
func (q *Queue) Get(k string) (Item, bool) {
93+
func (q *Queue) Get() (interface{}, error) {
9494
if q.IsEmpty() {
95-
return nil, false
95+
return nil, errors.New("queue is empty")
9696
}
9797

98-
item, exits := q.items.Load(k)
98+
q.rwMutex.RLock()
99+
key := q.keys[0]
100+
item, exits := q.items.Load(key)
101+
q.rwMutex.RUnlock()
99102
if !exits {
100-
return nil, false
101-
}
102-
103-
i, ok := (item).(Item)
104-
if !ok {
105-
return nil, false
103+
return nil, errors.New("this key does not exits")
106104
}
107105

108-
return i, true
106+
return item, nil
109107
}
110108

111-
// Front ...
112-
func (q *Queue) Front() *Item {
109+
// GetByKey ...
110+
func (q *Queue) GetByKey(k string) (interface{}, error) {
113111
if q.IsEmpty() {
114-
return nil
112+
return nil, errors.New("queue is empty")
115113
}
116114

117-
q.lock.RLock()
118-
key := q.keys[0]
119-
item, _ := q.items.Load(key)
120-
i, _ := item.(Item)
121-
q.lock.RUnlock()
115+
q.rwMutex.RLock()
116+
item, exits := q.items.Load(k)
117+
q.rwMutex.RUnlock()
118+
if !exits {
119+
return nil, errors.New("this key does not exits")
120+
}
122121

123-
return &i
122+
return item, nil
124123
}
125124

126125
// Range ...
@@ -137,3 +136,8 @@ func (q *Queue) IsEmpty() bool {
137136
func (q *Queue) Size() int {
138137
return len(q.keys)
139138
}
139+
140+
// Capacity ...
141+
func (q *Queue) Capacity() int64 {
142+
return q.queueCurrentSize
143+
}

redis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *RedisClient) Set(key string, value interface{}, expire time.Duration) e
7676
return r.Client.Set(key, value, expire).Err()
7777
}
7878

79-
// Get method return value based on the key provided
79+
// GetByKey method return value based on the key provided
8080
func (r *RedisClient) Get(key string) (interface{}, error) {
8181
return r.Client.Get(key).Result()
8282
}

0 commit comments

Comments
 (0)