fs

package
v13.38.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package fs provides cache with file system storage

Index

Examples

Constants

View Source
const DEFAULT_EXPIRATION = cache.HOUR

DEFAULT_EXPIRATION is default expiration

View Source
const DEFAULT_VALIDATION_REGEXP = `^[a-zA-Z0-9_-]{1,}$`

DEFAULT_VALIDATION_REGEXP is default regular expression pattern to validate keys

View Source
const MIN_CLEANUP_INTERVAL = cache.SECOND

MIN_CLEANUP_INTERVAL is minimal cleanup interval

View Source
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

func New(config Config) (*Cache, error)

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) All added in v13.35.0

func (c *Cache) All(yield func(k string, v any) bool)

All is an iterator over cache items

func (*Cache) Delete

func (c *Cache) Delete(key string) bool

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

func (c *Cache) Expired() int

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

func (c *Cache) Flush() bool

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

func (c *Cache) Get(key string) any

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

func (c *Cache) GetExpiration(key string) time.Time

GetWithExpiration returns item expiration date

func (*Cache) GetWithExpiration

func (c *Cache) GetWithExpiration(key string) (any, time.Time)

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

func (c *Cache) Has(key string) bool

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

func (c *Cache) Invalidate() bool

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()

func (*Cache) Keys added in v13.35.0

func (c *Cache) Keys(yield func(k string) bool)

Keys is an iterator over cache keys

func (*Cache) Set

func (c *Cache) Set(key string, data any, expiration ...cache.Duration) bool

Set adds or updates item in cache

Example
c, _ := New(Config{
	Dir:               "/path/to/cache",
	DefaultExpiration: cache.DAY,
})

c.Set("test", "ABCD")
c.Set("test", "ABCD", 15*cache.MINUTE)

fmt.Println(c.Get("test"))

func (*Cache) Size

func (c *Cache) Size() int

Size returns number of items in cache

Example
c, _ := New(Config{
	Dir:               "/path/to/cache",
	DefaultExpiration: cache.DAY,
})

c.Set("test", "ABCD")

fmt.Println(c.Size())

type Config

type Config struct {
	Dir               string
	ValidationRegexp  string
	DefaultExpiration cache.Duration
	CleanupInterval   cache.Duration
}

Config is cache configuration

func (Config) Validate

func (c Config) Validate() error

Validate validates cache configuration

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL