{"modulePath":"github.com/weiwolves/pkg","version":"v0.0.0-20200322065615-581c8d33cff6","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package objcache\n\nPackage objcache transcodes arbitrary Go types to bytes and stores them in a cache reducing GC.\n\nA Cache can be either in memory or a persistent one. Cache adapters are available for bigcache or Redis. To enable the cache adapter use build tags \"bigcache\" or \"redis\" or \"csall\". More cache adapters might follow.\n\nUse case: Caching millions of Go types as a byte slice reduces the pressure to the GC.\n\nFor more details regarding bigcache: [https://godoc.org/github.com/allegro/bigcache](https://godoc.org/github.com/allegro/bigcache)\n\n## Functions\n\n```go\nfunc MakeBinary() binary\n```\nMakeBinary creates a binary type for using in Set/Get functions when the code needs a \\`set\\` algorithm. For example checking if a JWT exists in the blacklist. Function IsValid returns true if the key exists.\n\n## Types\n\n```go\ntype Codecer interface {\n\tNewEncoder(io.Writer) Encoder\n\tNewDecoder(io.Reader) Decoder\n}\n```\nCodecer defines the functions needed to create a new Encoder or Decoder\n\n```go\ntype Decoder interface {\n\tDecode(dst interface{}) error\n}\n```\nDecoder defines how to decode a byte slice into variable dst. Please see option function WithEncoder() for details how to get the byte slice.\n\n```go\ntype Encoder interface {\n\tEncode(src interface{}) error\n}\n```\nEncoder defines how to Encode a type represented by variable src into a byte slice. Encoders must write their data into an io.Writer defined in option function WithEncoder().\n\n```go\ntype LRUOptions struct {\n\tCapacity           int64 // default 5000 objects\n\tTrackBySize        bool\n\tTrackByObjectCount bool // default\n\tLRUCache           *lru.Cache\n}\n```\nLRUOptions allows to track the cache items either by object count or total size in bytes. If tracking by \\`TrackBySize\\` gets enabled then \\`Capacity\\` must have the value in bytes. Default 64MB.\n\n```go\ntype NewStorageFn func() (Storager, error)\n```\n\n```go\nfunc NewBlackHoleClient(optionalTestErr error) NewStorageFn\n```\nNewBlackHoleClient creates a black hole client for testing with the ability to return errors.\n\n```go\nfunc NewLRU(o *LRUOptions) NewStorageFn\n```\nNewLRU creates a new LRU Storage. Expirations are not supported. Argument \\`o\\` can be nil, if so default values get applied.\n\n```go\ntype Service struct {\n\t// contains filtered or unexported fields\n}\n```\nService handles the encoding, decoding and caching.\n\n```go\nfunc NewService(level1, level2 NewStorageFn, so *ServiceOptions) (_ *Service, err error)\n```\nNewService creates a new cache service. Arguments level1 and level2 define the cache level. For example level1 should be an LRU or another in-memory cache while level2 should be accessed via network. Only level2 is requred while level1 can be nil.\n\n```go\nfunc (tr *Service) Close() error\n```\nClose closes the underlying storage engines.\n\n```go\nfunc (tr *Service) Delete(ctx context.Context, key ...string) error\n```\nDelete removes keys from the storage.\n\n```go\nfunc (tr *Service) Get(ctx context.Context, key string, dst interface{}) (err error)\n```\nGet looks up the key and parses the raw data into the destination pointer \\`dst\\`. If \\`dst\\` implements interface\n\n\ttype unmarshaler interface {\n\t\tUnmarshal([]byte) error\n\t}\n\nthe Unmarshal gets called (or one of the interface from package \\`encoding\\`). This type check has precedence before the decoder. You have to check yourself if the returned error is of type NotFound or of any other source. Every caching type defines its own NotFound error. If dst has no pointer property, no error gets returned, instead the passed value stays empty.\n\n```go\nfunc (tr *Service) GetMulti(ctx context.Context, keys []string, dst []interface{}) (err error)\n```\nGetMulti allows a cache backend to retrieve several values at once. Same decoding logic applies as when calling \\`Get\\`.\n\n```go\nfunc (tr *Service) Set(ctx context.Context, key string, src interface{}, expires time.Duration) error\n```\nSet puts the item in the cache. \\`src\\` gets either encoded using the previously applied encoder OR \\`src\\` gets checked if it implements interface\n\n\ttype marshaler interface {\n\t\tMarshal() ([]byte, error)\n\t}\n\nand calls \\`Marshal\\`. (also checks for the interfaces in package \"encoding\"). Checking for marshaler has precedence. Useful with protobuf.\n\n```go\nfunc (tr *Service) SetMulti(ctx context.Context, keys []string, src []interface{}, expires []time.Duration) error\n```\nSetMulti allows a cache to write several entities at once. For example using Redis MSET. Same logic applies as when using \\`Set\\`.\n\n```go\nfunc (tr *Service) Truncate(ctx context.Context) (err error)\n```\nTruncate truncates all caches.\n\n```go\ntype ServiceOptions struct {\n\t// Codec optionally encodes and decodes an object. The default \"codec\"\n\t// checks if a type implements Marshal() or Unmarshal() interface or any of\n\t// the two interfaces from package \"encoding\".\n\tCodec Codecer\n\t// PrimeObjects creates new encoder/decoder with a sync.Pool to reuse the\n\t// objects. Setting PrimeObjects causes the encoder/decode to prime the data\n\t// which means that no type information will be stored in the cache. If you\n\t// use gob you must use still gob.Register() your types. TL;DR: Skips type\n\t// information in the cache.\n\tPrimeObjects   []interface{}\n\tDefaultExpires time.Duration\n}\n```\nServiceOptions used when creating a NewService.\n\n```go\ntype Storager interface {\n\tSet(ctx context.Context, keys []string, values [][]byte, expirations []time.Duration) (err error)\n\t// Get returns the bytes for given keys. The values slice must have the same\n\t// length as the keys slice. If one of the keys can't be found, its byte\n\t// slice must be `nil`.\n\tGet(ctx context.Context, keys []string) (values [][]byte, err error)\n\tDelete(ctx context.Context, keys []string) (err error)\n\tTruncate(ctx context.Context) (err error)\n\tClose() error\n}\n```\nStorager defines a custom backend cache type to be used as underlying storage of the Service. Must be safe for concurrent usage. Caches which implement this interface can be enabled via build tag. The context depends if it is supported by a backend cache implementation. All keys and values have the same length. \\`expirations\\` is a list seconds with the same length as keys \u0026 values. A second entry defines when a key expires. If the entry is empty, the key does not expire.\n\n```go\nfunc NewCacheSimpleInmemory() (Storager, error)\n```\nNewCacheSimpleInmemory creates an in-memory map map\\[string]string as cache backend which supports expiration.\n\n","path":"github.com/weiwolves/pkg/storage/objcache","name":"objcache","synopsis":"Package objcache transcodes arbitrary Go types to bytes and stores them in a cache reducing GC.","isRedistributable":true}
