Decr decrease counter in runtime cache.
(key string)
| 155 | |
| 156 | // Decr decrease counter in runtime cache. |
| 157 | func (ca *RuntimeCache) Decr(key string) (int64, error) { |
| 158 | ca.Lock() |
| 159 | defer ca.Unlock() |
| 160 | itemObj, ok := ca.items.Load(key) |
| 161 | if !ok { |
| 162 | // if not exists, auto set new with 0 |
| 163 | ca.initValue(key, ZeroInt64, 0) |
| 164 | // reload |
| 165 | itemObj, _ = ca.items.Load(key) |
| 166 | } |
| 167 | |
| 168 | item := itemObj.(*RuntimeItem) |
| 169 | switch item.value.(type) { |
| 170 | case int: |
| 171 | item.value = item.value.(int) - 1 |
| 172 | case int64: |
| 173 | item.value = item.value.(int64) - 1 |
| 174 | case int32: |
| 175 | item.value = item.value.(int32) - 1 |
| 176 | case uint: |
| 177 | if item.value.(uint) > 0 { |
| 178 | item.value = item.value.(uint) - 1 |
| 179 | } else { |
| 180 | return 0, errors.New("item val is less than 0") |
| 181 | } |
| 182 | case uint32: |
| 183 | if item.value.(uint32) > 0 { |
| 184 | item.value = item.value.(uint32) - 1 |
| 185 | } else { |
| 186 | return 0, errors.New("item val is less than 0") |
| 187 | } |
| 188 | case uint64: |
| 189 | if item.value.(uint64) > 0 { |
| 190 | item.value = item.value.(uint64) - 1 |
| 191 | } else { |
| 192 | return 0, errors.New("item val is less than 0") |
| 193 | } |
| 194 | default: |
| 195 | return 0, errors.New("item val is not int int64 int32") |
| 196 | } |
| 197 | |
| 198 | val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64) |
| 199 | return val, nil |
| 200 | } |
| 201 | |
| 202 | // Exist check item exist in runtime cache. |
| 203 | func (ca *RuntimeCache) Exists(key string) (bool, error) { |