TTL returns the time-to-live (TTL) of a key in the hash. It takes a string key 'k' and returns the remaining TTL in seconds and any possible error. Parameters: k: The key for which TTL needs to be determined. Returns: int64: The remaining TTL in seconds. Returns 0 if the key has expired or doe
(key string)
| 1280 | // int64: The remaining TTL in seconds. Returns 0 if the key has expired or doesn't exist. |
| 1281 | // error: An error if occurred during the operation, or nil on success. |
| 1282 | func (hs *HashStructure) TTL(key string) (int64, error) { |
| 1283 | // Check the parameters |
| 1284 | if len(key) == 0 { |
| 1285 | return -1, _const.ErrKeyIsEmpty |
| 1286 | } |
| 1287 | |
| 1288 | // Find the hash metadata by the given key |
| 1289 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 1290 | if err != nil { |
| 1291 | return -1, err |
| 1292 | } |
| 1293 | |
| 1294 | // If the counter is 0, return empty string |
| 1295 | if hashMeta.counter == 0 { |
| 1296 | return 0, _const.ErrKeyNotFound |
| 1297 | } |
| 1298 | |
| 1299 | // Calculate the TTL |
| 1300 | ttl := hashMeta.expire/int64(time.Second) - time.Now().UnixNano()/int64(time.Second) |
| 1301 | |
| 1302 | // If the TTL is 0, return 0 |
| 1303 | if hashMeta.expire == 0 { |
| 1304 | return 0, nil |
| 1305 | } |
| 1306 | |
| 1307 | // If the TTL is less than 0, return -1 |
| 1308 | if ttl <= 0 { |
| 1309 | return -1, _const.ErrKeyIsExpired |
| 1310 | } |
| 1311 | return ttl, nil |
| 1312 | } |
| 1313 | |
| 1314 | // Size returns the size of a field in the hash as a formatted string. |
| 1315 | // It takes a string key 'k' and one or more fields 'f' (optional). |
no test coverage detected