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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*
* Copyright 2012-2022 Li Kexian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* A toolkit for Golang development
* https://www.likexian.com/
*/
package memory
import (
"errors"
"sync"
"time"
)
var (
// ErrKeyNotExists is key not exists error
ErrKeyNotExists = errors.New("xcache: the key is not exists")
// ErrDataTypeNotSupported is data type not supported error
ErrDataTypeNotSupported = errors.New("xcache: data type is not supported")
// ErrValueLessThanZero is value less than zero error
ErrValueLessThanZero = errors.New("xcache: object value is less than zero")
)
// Object is storing single object
type Object struct {
value interface{}
expire int64
}
// Objects is storing all object
type Objects struct {
values map[string]*Object
gcInterval int
gcMaxOnce int
gcExit chan int
sync.RWMutex
}
// Version returns package version
func Version() string {
return "0.2.0"
}
// Author returns package author
func Author() string {
return "[Li Kexian](https://www.likexian.com/)"
}
// License returns package license
func License() string {
return "Licensed under the Apache License 2.0"
}
// New init a new cache
func New() *Objects {
o := &Objects{
values: map[string]*Object{},
gcInterval: 60,
gcMaxOnce: 100,
gcExit: make(chan int),
}
go o.gc()
return o
}
// Set set key value to cache
func (o *Objects) Set(key string, val interface{}, ttl int64) error {
if ttl > 0 {
ttl = time.Now().Add(time.Duration(ttl) * time.Second).Unix()
}
o.Lock()
defer o.Unlock()
o.values[key] = &Object{val, ttl}
return nil
}
// Get get value from cache
func (o *Objects) Get(key string) interface{} {
o.RLock()
defer o.RUnlock()
v, ok := o.values[key]
if !ok {
return nil
}
if v.expired() {
return nil
}
return v.value
}
// MGet get multiple value from cache
func (o *Objects) MGet(key ...string) []interface{} {
r := []interface{}{}
for _, k := range key {
r = append(r, o.Get(k))
}
return r
}
// Has returns key is exists
func (o *Objects) Has(key string) bool {
o.RLock()
defer o.RUnlock()
v, ok := o.values[key]
if !ok {
return false
}
return !v.expired()
}
// Del remove key from cache
func (o *Objects) Del(key string) error {
o.Lock()
defer o.Unlock()
delete(o.values, key)
return nil
}
// Incr increase cache counter
func (o *Objects) Incr(key string) error {
o.Lock()
defer o.Unlock()
v, ok := o.values[key]
if !ok {
return ErrKeyNotExists
}
switch vv := v.value.(type) {
case int:
v.value = vv + 1
case int32:
v.value = vv + 1
case int64:
v.value = vv + 1
case uint:
v.value = vv + 1
case uint32:
v.value = vv + 1
case uint64:
v.value = vv + 1
default:
return ErrDataTypeNotSupported
}
return nil
}
// Decr decrease cache counter
func (o *Objects) Decr(key string) error {
o.Lock()
defer o.Unlock()
v, ok := o.values[key]
if !ok {
return ErrKeyNotExists
}
switch vv := v.value.(type) {
case int:
v.value = vv - 1
case int32:
v.value = vv - 1
case int64:
v.value = vv - 1
case uint:
if vv <= 0 {
return ErrValueLessThanZero
}
v.value = vv - 1
case uint32:
if vv <= 0 {
return ErrValueLessThanZero
}
v.value = vv - 1
case uint64:
if vv <= 0 {
return ErrValueLessThanZero
}
v.value = vv - 1
default:
return ErrDataTypeNotSupported
}
return nil
}
// Flush empty the cache
func (o *Objects) Flush() error {
o.Lock()
defer o.Unlock()
o.values = map[string]*Object{}
return nil
}
// Close stop the cache service
func (o *Objects) Close() error {
o.gcExit <- 1
o.Flush()
return nil
}
// SetGC set gc interval and max once
func (o *Objects) SetGC(gcInterval, gcMaxOnce int) {
o.Lock()
o.gcInterval = gcInterval
o.gcMaxOnce = gcMaxOnce
o.Unlock()
o.gcExit <- 1
go o.gc()
}
// gc do gc check
func (o *Objects) gc() {
o.RLock()
gcInterval := o.gcInterval
o.RUnlock()
t := time.NewTicker(time.Duration(gcInterval) * time.Second)
for {
select {
case <-o.gcExit:
t.Stop()
return
case <-t.C:
o.RLock()
e := []string{}
for k, v := range o.values {
if v.expired() {
e = append(e, k)
if len(e) >= o.gcMaxOnce {
break
}
}
}
o.RUnlock()
o.Lock()
for _, k := range e {
delete(o.values, k)
}
o.Unlock()
}
}
}
// expired returns object is expired
func (b *Object) expired() bool {
if b.expire <= 0 {
return false
}
return time.Now().Unix() >= b.expire
}
|