github.com/goravel/framework@v1.13.9/contracts/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  //go:generate mockery --name=Cache
     9  type Cache interface {
    10  	Driver
    11  	Store(name string) Driver
    12  }
    13  
    14  //go:generate mockery --name=Driver
    15  type Driver interface {
    16  	// Add an item in the cache if the key does not exist.
    17  	Add(key string, value any, t time.Duration) bool
    18  	// Decrement decrements the value of an item in the cache.
    19  	Decrement(key string, value ...int) (int, error)
    20  	// Forever add an item in the cache indefinitely.
    21  	Forever(key string, value any) bool
    22  	// Forget removes an item from the cache.
    23  	Forget(key string) bool
    24  	// Flush remove all items from the cache.
    25  	Flush() bool
    26  	// Get retrieve an item from the cache by key.
    27  	Get(key string, def ...any) any
    28  	// GetBool retrieves an item from the cache by key as a boolean.
    29  	GetBool(key string, def ...bool) bool
    30  	// GetInt retrieves an item from the cache by key as an integer.
    31  	GetInt(key string, def ...int) int
    32  	// GetInt64 retrieves an item from the cache by key as a 64-bit integer.
    33  	GetInt64(key string, def ...int64) int64
    34  	// GetString retrieves an item from the cache by key as a string.
    35  	GetString(key string, def ...string) string
    36  	// Has check an item exists in the cache.
    37  	Has(key string) bool
    38  	// Increment increments the value of an item in the cache.
    39  	Increment(key string, value ...int) (int, error)
    40  	// Lock get a lock instance.
    41  	Lock(key string, t ...time.Duration) Lock
    42  	// Put Driver an item in the cache for a given time.
    43  	Put(key string, value any, t time.Duration) error
    44  	// Pull retrieve an item from the cache and delete it.
    45  	Pull(key string, def ...any) any
    46  	// Remember gets an item from the cache, or execute the given Closure and store the result.
    47  	Remember(key string, ttl time.Duration, callback func() (any, error)) (any, error)
    48  	// RememberForever get an item from the cache, or execute the given Closure and store the result forever.
    49  	RememberForever(key string, callback func() (any, error)) (any, error)
    50  	// WithContext returns a new Cache instance with the given context.
    51  	WithContext(ctx context.Context) Driver
    52  }
    53  
    54  //go:generate mockery --name=Lock
    55  type Lock interface {
    56  	// Block attempt to acquire the lock for the given number of seconds.
    57  	Block(t time.Duration, callback ...func()) bool
    58  	// Get attempts to acquire the lock.
    59  	Get(callback ...func()) bool
    60  	// Release the lock.
    61  	Release() bool
    62  	// ForceRelease releases the lock in disregard of ownership.
    63  	ForceRelease() bool
    64  }