helper

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: AGPL-3.0, MIT Imports: 11 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCachePath

func GetCachePath(pluginName string) string

Find the directory where the plugin can cache data

It is recommended to use this function to get the cache directory because the SQL function clear_plugin_cache(plugin_name) will clear the directory returned by this function

Internally, this function uses the XDG_CACHE_HOME environment variable so that anyone can override the cache directory to its needs

func Serialize added in v0.1.3

func Serialize(v interface{}) interface{}

Serialize a value to a type that can be sent to Anyquery, and respecting Anyquery guidelines

This is useful to not bother with nil-checks, JSON serialization, etc. when returning values from the plugin

Internally, because Anyquery uses JSON for any non-primitive types, this function will convert any non-primitive types to JSON

  • When it encounters a pointer, it will return nil if the pointer is nil or the value if the pointer is not nil
  • When it encounters a slice, it will return nil if the slice is empty or the JSON representation of the slice if it is not empty
  • When it encounters a time.Time, it will return the RFC3339 representation of the time
  • Primitive types are returned as is

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a simple key-value store that can store metadata with rows

It is the recommended way to cache data in a plugin because it abstracts away the underlying storage and encryption

It also helps the user to clear the cache with the SQL function clear_plugin_cache(plugin_name)

func NewCache

func NewCache(args NewCacheArgs) (*Cache, error)

Create a new cache at $XDG_CACHE_HOME/anyquery/paths[0]/.../paths[n]

func (*Cache) Clear

func (c *Cache) Clear() error

Clear all the keys in the cache

func (*Cache) ClearWithPrefix

func (c *Cache) ClearWithPrefix(prefix string) error

func (*Cache) Close

func (c *Cache) Close() error

func (*Cache) Delete

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

func (*Cache) Get

func (c *Cache) Get(key string) ([][]interface{}, map[string]interface{}, error)

Get the value and the metadata of the key in the cache

func (*Cache) Set

func (c *Cache) Set(key string, value [][]interface{}, metadata map[string]interface{},
	ttl time.Duration) error

Save the key in the cache with the value and the metadata for a duration of ttl (default to time.Hour if zero)

type NewCacheArgs

type NewCacheArgs struct {
	// Where the cache will be stored (must be a unique set of paths for all running instances of the plugin).
	// Often, it's the plugin name followed by the MD5 hash of current user for an API. The last path should not
	// be a directory but a file.
	//
	// The cache will be stored at $XDG_CACHE_HOME/anyquery/paths[0]/.../paths[n]
	//
	// For example, []string{"trello", "boards", "5f4dcc3b5aa765d61d8327deb882cf99"} will store the cache at
	// $XDG_CACHE_HOME/anyquery/plugins/trello/boards/5f4dcc3b5aa765d61d8327deb882cf99
	Paths []string

	// The maximum on-disk size of the cache in bytes
	//
	// Default to 64MB (1 << 26)
	MaxSize int64

	// The maximum in-memory size of the cache of the cache in bytes
	//
	// Default to 8MB (1 << 23)
	MaxMemSize int64

	// An encryption key to encrypt the cache (required for security)
	//
	// The key must be 16, 24 or 32 bytes long for AES-128, AES-192 and AES-256 respectively
	EncryptionKey []byte
}

Jump to

Keyboard shortcuts

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