fs

package
v13.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package fs provides cache with file system storage

Index

Examples

Constants

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,
})

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

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

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,
	CleanupInterval:   cache.MINUTE,
})

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

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

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,
	CleanupInterval:   cache.MINUTE,
})

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

fmt.Println(c.Expired())
Output:

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,
	CleanupInterval:   cache.MINUTE,
})

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

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

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,
	CleanupInterval:   cache.MINUTE,
})

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

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

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,
	CleanupInterval:   cache.MINUTE,
})

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

item, exp := c.GetWithExpiration("test")

fmt.Println(item, exp.String())
Output:

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,
	CleanupInterval:   cache.MINUTE,
})

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

fmt.Println(c.Has("test"))
Output:

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,
	CleanupInterval:   cache.MINUTE,
})

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

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

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,
	CleanupInterval:   cache.MINUTE,
})

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

fmt.Println(c.Size())
Output:

type Config

type Config struct {
	Dir               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