HExpire sets a timeout on a hash. It takes a string key 'k' and a duration 'ttl' to set the timeout. It returns a boolean indicating the success of the operation and any possible error. Parameters: k: The key of the hash table. ttl: The duration of the timeout. Returns: bool: True if the time
(key string, ttl int64)
| 429 | // bool: True if the timeout was set successfully, false otherwise. |
| 430 | // error: An error if occurred during the operation, or nil on success. |
| 431 | func (hs *HashStructure) HExpire(key string, ttl int64) (bool, error) { |
| 432 | // Convert the parameters to bytes |
| 433 | k := stringToBytesWithKey(key) |
| 434 | |
| 435 | // Check the parameters |
| 436 | if len(k) == 0 { |
| 437 | return false, _const.ErrKeyIsEmpty |
| 438 | } |
| 439 | |
| 440 | // Find the hash metadata by the given key |
| 441 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 442 | if err != nil { |
| 443 | return false, err |
| 444 | } |
| 445 | |
| 446 | // If the counter is 0, return false |
| 447 | if hashMeta.counter == 0 { |
| 448 | return false, nil |
| 449 | } |
| 450 | |
| 451 | // Create a new write batch |
| 452 | batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions) |
| 453 | |
| 454 | // Put the updated hash metadata to the database |
| 455 | hashMeta.expire = time.Now().Add(time.Duration(ttl) * time.Second).UnixNano() |
| 456 | _ = batch.Put(k, hashMeta.encodeHashMeta()) |
| 457 | |
| 458 | // Commit the write batch |
| 459 | err = batch.Commit() |
| 460 | if err != nil { |
| 461 | return false, err |
| 462 | } |
| 463 | |
| 464 | return true, nil |
| 465 | } |
| 466 | |
| 467 | // HExists determines whether a hash field exists or not. |
| 468 | // It takes a string key 'k' and a field 'f' to check for existence. |
nothing calls this directly
no test coverage detected