Documentation
¶
Index ¶
- type Config
- type Querier
- type Simple
- func (s *Simple) Close() error
- func (s *Simple) Delete(key string)
- func (s *Simple) GC()
- func (s *Simple) Load(key string) (interface{}, bool)
- func (s *Simple) Query(key string) (interface{}, error)
- func (s *Simple) Range(callback func(key string, value *TimedValue))
- func (s *Simple) Stats() Stats
- func (s *Simple) Store(key string, value interface{})
- func (s *Simple) Update(key string)
- type Stats
- type TimedValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// GoodStaleDuration specifies how long a value remains fresh in the data map. A zero-value
// time.Duration value implies the value never stales, and updates will only be fetched
// after the value expires.
GoodStaleDuration time.Duration
// GoodExpiryDuration specifies how long a value is allowed to be served from the data
// map. A zero-value time.Duration value implies the value never expires and can always be
// served.
GoodExpiryDuration time.Duration
// BadStaleDuration specifies how long an error result remains fresh in the data map. A
// zero-value time.Duration value implies that the error result is stale as soon as it's
// inserted into the data map, and Querying the key again will trigger an asynchronous
// lookup of the key to try to replace the error value with a good value.
BadStaleDuration time.Duration
// BadExpiryDuration specifies how long an error result is allowed to be served from the
// data map. A zero-value time.Duration value implies the value never expires and can always
// be served.
BadExpiryDuration time.Duration
// Lookup specifies the user callback function to invoke when looking up the value to be
// associated with a stale key, expired key, or a key that has yet to be loaded into the
// data map.
Lookup func(string) (interface{}, error)
// GCPeriodicity specifies how frequently the data map purges expired entries.
GCPeriodicity time.Duration
// GCTimeout specifies how long the GC should wait for values to unlock.
GCTimeout time.Duration
}
Config specifies the configuration parameters for a Swarm instance.
type Querier ¶
Querier specifies a type that provides memoizing the results of expensive function calls and returning the cached result when the same input key occurs again.
type Simple ¶
type Simple struct {
// contains filtered or unexported fields
}
Simple memoizes responses from a Querier, providing very low-level time-based control of how values go stale or expire. When a new value is stored in the Simple instance, if it is a TimedValue item--or a pointer to a TimedValue item)--the data map will use the provided Stale and Expiry values. If the new value is not a TimedValue instance or pointer to a TimedValue instance, then the Simple instance wraps the value in a TimedValue struct, and adds the Simple instance's stale and expiry durations to the current time and stores the resultant TimedValue instance.
func NewSimple ¶
NewSimple returns Swarm that attempts to respond to Query methods by consulting its TTL cache, then directing the call to the underlying Querier if a valid response is not stored. Note this function accepts a pointer so creating an instance with defaults can be done by passing a nil value rather than a pointer to a Config instance.
simple, err := goswarm.NewSimple(&goswarm.Simple{
GoodStaleDuration: time.Minute,
GoodExpiryDuration: 24 * time.Hour,
BadStaleDuration: time.Minute,
BadExpiryDuration: 5 * time.Minute,
Lookup: func(key string) (interface{}, error) {
// TODO: do slow calculation or make a network call
result := key // example
return result, nil
},
})
if err != nil {
log.Fatal(err)
}
defer func() { _ = simple.Close() }()
func (*Simple) Close ¶
Close releases all memory and go-routines used by the Simple swarm. If during instantiation, GCPeriodicity was greater than the zero-value for time.Duration, this method may block while completing any in progress GC run.
func (*Simple) GC ¶
func (s *Simple) GC()
GC examines all key value pairs in the Simple swarm and deletes those whose values have expired.
func (*Simple) Load ¶
Load returns the value associated with the specified key, and a boolean value indicating whether or not the key was found in the map.
func (*Simple) Query ¶
Query loads the value associated with the specified key from the data map. When a stale value is found on Query, at most one asynchronous lookup of a new value is triggered, and the current value is returned from the data map. When no value or an expired value is found on Query, a synchronous lookup of a new value is triggered, then the new value is stored and returned.
func (*Simple) Range ¶ added in v1.1.0
func (s *Simple) Range(callback func(key string, value *TimedValue))
Range invokes specified callback function for each non-expired key in the data map. Each key-value pair is independently locked until the callback function invoked with the specified key returns. This method does not block access to the Simple instance, allowing keys to be added and removed like normal even while the callbacks are running.
func (*Simple) Stats ¶ added in v1.6.0
Stats returns a snapshot of the cache's statistics. Note that except for the Size statistic, all other statistics will be reset when this method is invoked, allowing the client to determine the number of each respective events that have taken place since the previous time this method was invoked.
type Stats ¶ added in v1.6.0
type Stats struct {
Creates int64 // Creates represents how many new cache items were created since the previous Stats call.
Deletes int64 // Deletes represents how many Delete calls were made since the previous Stats call.
Evictions int64 // Evictions represents how many evictions took place since the previous Stats call.
Loads int64 // Loads represents how many Load calls were made since the previous Stats call.
LookupErrors int64 // LookupErrors represents the number of Lookups invocations that returned an error since the previous Stats call.
Queries int64 // Queries represents how many Query calls were made since the previous Stats call.
QueriesFresh int64 // QueriesFresh represents how many Query calls were answered with fresh data.
QueriesMiss int64 // QueriesMiss represents how many Query calls were
QueriesStale int64 // QueriesStale represents how many Query calls were answered with stale data.
Size int64 // Size represents the number of items in the cache.
Stores int64 // Stores represents how many Store calls were made since the previous Stats call.
Updates int64 // Updates represents how many Update calls were made since the previous Stats call.
}
Stats contains various cache statistics.
type TimedValue ¶
type TimedValue struct {
// Value stores the datum returned by the lookup function.
Value interface{}
// Err stores the error returned by the lookup function.
Err error
// Stale stores the time at which the value becomes stale. On Query, a stale value will
// trigger an asynchronous lookup of a replacement value, and the original value is
// returned. A zero-value for Stale implies the value never goes stale, and querying the key
// associated for this value will never trigger an asynchronous lookup of a replacement
// value.
Stale time.Time
// Expiry stores the time at which the value expires. On Query, an expired value will block
// until a synchronous lookup of a replacement value is attempted. Once the lookup returns,
// the Query method will return with the new value or the error returned by the lookup
// function.
Expiry time.Time
}
TimedValue couples a value or the error with both a stale and expiry time for the value and error.
func (*TimedValue) IsExpired ¶ added in v1.2.0
func (tv *TimedValue) IsExpired() bool
IsExpired returns true if and only if value is expired. A value is expired when its non-zero expiry time is before the current time, or when the value represents an error and expiry time is the time.Time zero-value.
func (*TimedValue) IsStale ¶ added in v1.2.0
func (tv *TimedValue) IsStale() bool
IsStale returns true if and only if value is stale. A value is stale when its non-zero stale time is before the current time, or when the value represents an error and stale time is the time.Time zero-value.