github.com/goravel/framework@v1.13.9/cache/memory.go (about) 1 package cache 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/patrickmn/go-cache" 8 "github.com/spf13/cast" 9 10 contractscache "github.com/goravel/framework/contracts/cache" 11 "github.com/goravel/framework/contracts/config" 12 ) 13 14 type Memory struct { 15 ctx context.Context 16 prefix string 17 instance *cache.Cache 18 } 19 20 func NewMemory(config config.Config) (*Memory, error) { 21 memory := cache.New(5*time.Minute, 10*time.Minute) 22 23 return &Memory{ 24 prefix: prefix(config), 25 instance: memory, 26 }, nil 27 } 28 29 //Add Driver an item in the cache if the key does not exist. 30 func (r *Memory) Add(key string, value any, t time.Duration) bool { 31 if t == NoExpiration { 32 t = cache.NoExpiration 33 } 34 35 err := r.instance.Add(r.key(key), value, t) 36 37 return err == nil 38 } 39 40 func (r *Memory) Decrement(key string, value ...int) (int, error) { 41 if len(value) == 0 { 42 value = append(value, 1) 43 } 44 r.Add(key, 0, cache.NoExpiration) 45 46 return r.instance.DecrementInt(r.key(key), value[0]) 47 } 48 49 //Forever Driver an item in the cache indefinitely. 50 func (r *Memory) Forever(key string, value any) bool { 51 if err := r.Put(key, value, cache.NoExpiration); err != nil { 52 return false 53 } 54 55 return true 56 } 57 58 //Forget Remove an item from the cache. 59 func (r *Memory) Forget(key string) bool { 60 r.instance.Delete(r.key(key)) 61 62 return true 63 } 64 65 //Flush Remove all items from the cache. 66 func (r *Memory) Flush() bool { 67 r.instance.Flush() 68 69 return true 70 } 71 72 //Get Retrieve an item from the cache by key. 73 func (r *Memory) Get(key string, def ...any) any { 74 val, exist := r.instance.Get(r.key(key)) 75 if exist { 76 return val 77 } 78 if len(def) == 0 { 79 return nil 80 } 81 82 switch s := def[0].(type) { 83 case func() any: 84 return s() 85 default: 86 return s 87 } 88 } 89 90 func (r *Memory) GetBool(key string, def ...bool) bool { 91 if len(def) == 0 { 92 def = append(def, false) 93 } 94 res := r.Get(key, def[0]) 95 96 return cast.ToBool(res) 97 } 98 99 func (r *Memory) GetInt(key string, def ...int) int { 100 if len(def) == 0 { 101 def = append(def, 0) 102 } 103 104 return cast.ToInt(r.Get(key, def[0])) 105 } 106 107 func (r *Memory) GetInt64(key string, def ...int64) int64 { 108 if len(def) == 0 { 109 def = append(def, 0) 110 } 111 112 return cast.ToInt64(r.Get(key, def[0])) 113 } 114 115 func (r *Memory) GetString(key string, def ...string) string { 116 if len(def) == 0 { 117 def = append(def, "") 118 } 119 120 return cast.ToString(r.Get(key, def[0])) 121 } 122 123 //Has Check an item exists in the cache. 124 func (r *Memory) Has(key string) bool { 125 _, exist := r.instance.Get(r.key(key)) 126 127 return exist 128 } 129 130 func (r *Memory) Increment(key string, value ...int) (int, error) { 131 if len(value) == 0 { 132 value = append(value, 1) 133 } 134 r.Add(key, 0, cache.NoExpiration) 135 136 return r.instance.IncrementInt(r.key(key), value[0]) 137 } 138 139 func (r *Memory) Lock(key string, t ...time.Duration) contractscache.Lock { 140 return NewLock(r, key, t...) 141 } 142 143 //Pull Retrieve an item from the cache and delete it. 144 func (r *Memory) Pull(key string, def ...any) any { 145 var res any 146 if len(def) == 0 { 147 res = r.Get(key) 148 } else { 149 res = r.Get(key, def[0]) 150 } 151 r.Forget(key) 152 153 return res 154 } 155 156 //Put Driver an item in the cache for a given number of seconds. 157 func (r *Memory) Put(key string, value any, t time.Duration) error { 158 r.instance.Set(r.key(key), value, t) 159 160 return nil 161 } 162 163 //Remember Get an item from the cache, or execute the given Closure and store the result. 164 func (r *Memory) Remember(key string, seconds time.Duration, callback func() (any, error)) (any, error) { 165 val := r.Get(key, nil) 166 if val != nil { 167 return val, nil 168 } 169 170 var err error 171 val, err = callback() 172 if err != nil { 173 return nil, err 174 } 175 176 if err := r.Put(key, val, seconds); err != nil { 177 return nil, err 178 } 179 180 return val, nil 181 } 182 183 //RememberForever Get an item from the cache, or execute the given Closure and store the result forever. 184 func (r *Memory) RememberForever(key string, callback func() (any, error)) (any, error) { 185 val := r.Get(key, nil) 186 if val != nil { 187 return val, nil 188 } 189 190 var err error 191 val, err = callback() 192 if err != nil { 193 return nil, err 194 } 195 196 if err := r.Put(key, val, cache.NoExpiration); err != nil { 197 return nil, err 198 } 199 200 return val, nil 201 } 202 203 func (r *Memory) WithContext(ctx context.Context) contractscache.Driver { 204 r.ctx = ctx 205 206 return r 207 } 208 209 func (r *Memory) key(key string) string { 210 return r.prefix + key 211 }