expire.go
expire.go - Overview
This file manages the expiration of objects stored in the Store
. It includes functions to check if an object has expired, retrieve its expiry time, delete the expiry, and actively delete expired keys. It also handles setting the expiry with different conditions like NX, XX, GT, and LT.
Detailed Documentation
Constants
const (
NX string = "NX"
XX string = "XX"
GT string = "GT"
LT string = "LT"
)
These constants define the subcommands for setting expiry conditions.
NX
: Set the expiration only if the key does not already have an expiration time.XX
: Set the expiration only if the key already has an expiration time.GT
: Set the expiration only if the new expiration time is greater than the current one.LT
: Set the expiration only if the new expiration time is less than the current one.
Function hasExpired
func hasExpired(obj *object.Obj, store *Store) bool
Purpose: Checks if an object has expired.
Parameters:
obj
(*object.Obj): The object to check.store
(*Store): The store where the object is stored.
Returns:
bool
: Returnstrue
if the object has expired,false
otherwise.
Function GetExpiry
func GetExpiry(obj *object.Obj, store *Store) (uint64, bool)
Purpose: Retrieves the expiry time of an object.
Parameters:
obj
(*object.Obj): The object to get the expiry time for.store
(*Store): The store where the object is stored.
Returns:
uint64
: The expiry time in milliseconds since the Unix epoch.bool
:true
if the object has an expiry time,false
otherwise.
Function DelExpiry
func DelExpiry(obj *object.Obj, store *Store)
Purpose: Deletes the expiry time of an object.
Parameters:
obj
(*object.Obj): The object to delete the expiry time for.store
(*Store): The store where the object is stored.
Returns:
- None
Function expireSample
func expireSample(store *Store) float32
Purpose: Samples a limited number of keys from the store and deletes the expired ones.
Parameters:
store
(*Store): The store to sample from.
Returns:
float32
: The fraction of expired keys in the sample.
Function DeleteExpiredKeys
func DeleteExpiredKeys(store *Store)
Purpose: Deletes all expired keys in the store by repeatedly sampling until the fraction of expired keys is below a threshold.
Parameters:
store
(*Store): The store to delete expired keys from.
Returns:
- None
Function EvaluateAndSetExpiry
func EvaluateAndSetExpiry(subCommands []string, newExpiry int64, key string,
store *Store) (shouldSetExpiry bool, err error)
Purpose: Evaluates the given subcommands (NX, XX, GT, LT) and sets the expiry of a key based on the conditions.
Parameters:
subCommands
([]string): A slice of strings representing the subcommands.newExpiry
(int64): The new expiry time in seconds since the Unix epoch.key
(string): The key to set the expiry for.store
(*Store): The store where the object is stored.
Returns:
shouldSetExpiry
(bool):true
if the expiry should be set,false
otherwise.err
(error): An error if there is an invalid combination of subcommands or an unsupported subcommand, nil otherwise.