Incr increase int64 counter in runtime cache.
(key string)
| 121 | |
| 122 | // Incr increase int64 counter in runtime cache. |
| 123 | func (ca *RuntimeCache) Incr(key string) (int64, error) { |
| 124 | ca.Lock() |
| 125 | defer ca.Unlock() |
| 126 | itemObj, ok := ca.items.Load(key) |
| 127 | if !ok { |
| 128 | // if not exists, auto set new with 0 |
| 129 | ca.initValue(key, ZeroInt64, 0) |
| 130 | // reload |
| 131 | itemObj, _ = ca.items.Load(key) |
| 132 | } |
| 133 | |
| 134 | item := itemObj.(*RuntimeItem) |
| 135 | switch item.value.(type) { |
| 136 | case int: |
| 137 | item.value = item.value.(int) + 1 |
| 138 | case int32: |
| 139 | item.value = item.value.(int32) + 1 |
| 140 | case int64: |
| 141 | item.value = item.value.(int64) + 1 |
| 142 | case uint: |
| 143 | item.value = item.value.(uint) + 1 |
| 144 | case uint32: |
| 145 | item.value = item.value.(uint32) + 1 |
| 146 | case uint64: |
| 147 | item.value = item.value.(uint64) + 1 |
| 148 | default: |
| 149 | return 0, errors.New("item val is not (u)int (u)int32 (u)int64") |
| 150 | } |
| 151 | |
| 152 | val, _ := strconv.ParseInt(fmt.Sprint(item.value), 10, 64) |
| 153 | return val, nil |
| 154 | } |
| 155 | |
| 156 | // Decr decrease counter in runtime cache. |
| 157 | func (ca *RuntimeCache) Decr(key string) (int64, error) { |