Documentation
¶
Overview ¶
Package fs provides cache with file system storage
Index ¶
- Constants
- type Cache
- func (c *Cache) All(yield func(k string, v any) bool)
- func (c *Cache) Delete(key string) bool
- func (c *Cache) Expired() int
- func (c *Cache) Flush() bool
- func (c *Cache) Get(key string) any
- func (c *Cache) GetExpiration(key string) time.Time
- func (c *Cache) GetWithExpiration(key string) (any, time.Time)
- func (c *Cache) Has(key string) bool
- func (c *Cache) Invalidate() bool
- func (c *Cache) Keys(yield func(k string) bool)
- func (c *Cache) Set(key string, data any, expiration ...cache.Duration) bool
- func (c *Cache) Size() int
- type Config
Examples ¶
Constants ¶
const DEFAULT_EXPIRATION = cache.HOUR
DEFAULT_EXPIRATION is default expiration
const DEFAULT_VALIDATION_REGEXP = `^[a-zA-Z0-9_-]{1,}$`
DEFAULT_VALIDATION_REGEXP is default regular expression pattern to validate keys
const MIN_CLEANUP_INTERVAL = cache.SECOND
MIN_CLEANUP_INTERVAL is minimal cleanup interval
const MIN_EXPIRATION = cache.SECOND
MIN_EXPIRATION is minimal expiration duration
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is fs cache instance
func New ¶
New creates new cache instance
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
CleanupInterval: cache.MINUTE,
ValidationRegexp: `^[a-fA-F0-9]{8}$`,
})
c.Set("045d01c1", "Test data")
fmt.Println(c.Get("test"))
func (*Cache) Delete ¶
Delete removes item from cache
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
c.Delete("test")
fmt.Println(c.Get("test"))
func (*Cache) Expired ¶
Expired returns number of expired items in cache
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
fmt.Println(c.Expired())
func (*Cache) Flush ¶
Flush removes all data from cache
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
c.Flush()
fmt.Println(c.Get("test"))
func (*Cache) Get ¶
GetWithExpiration returns item from cache
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
fmt.Println(c.Get("test"))
func (*Cache) GetExpiration ¶
GetWithExpiration returns item expiration date
func (*Cache) GetWithExpiration ¶
GetWithExpiration returns item from cache and expiration date or nil
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
item, exp := c.GetWithExpiration("test")
fmt.Println(item, exp.String())
func (*Cache) Has ¶
Has returns true if cache contains data for given key
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
fmt.Println(c.Has("test"))
func (*Cache) Invalidate ¶ added in v13.35.0
Invalidate deletes all expired records
Example ¶
c, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: cache.DAY,
})
c.Set("test", "ABCD")
// ... some time after
c.Invalidate()