package
1.1.3
Repository: https://github.com/corsc/go-commons.git
Documentation: pkg.go.dev

# README

Cache

Simple cache implementation with pluggable storage; optional logging and instrumentation.

For usage examples please refer here

Redis storage

  • This library makes no effort to ensure it does not overwrite other data in the server. Key names should be chosen carefully

Tests

Tests that require Redis or DDB are protected with environment variable flags.

Use REDIS=true go test ./... to run Redis tests. (These tests assume redis running on :6379)

Use DDB=true go test ./... to run DDB tests. (These tests assume redis running on :6379)

(These tests use DynamoDbLocal and assume server running on https://s3-ap-southeast-1.amazonaws.com/dynamodb-local-singapore/release)

DynamoDB storage

  • TTL should be enabled on the table with attribute name ttl see reference

Notes:

Logging

While this package does log, it only logs asynchronous errors. Personally I would prefer not to log at all but this would leave async issues completely invisible.

That said, logging is optional.

Metrics

Metrics are provided but optional.

# Constants

CacheGetError denotes an error occurred with the cache or underlying storage (during gets).
CacheHit denotes the key was found in the cache and successfully returned.
CacheInvalidateError denotes an error occurred with the cache or underlying storage (during invalidates).
CacheLambdaError denotes an error occurred in the user code (e.g.
CacheMarshalError denotes an error occurred in while calling BinaryEncoder.MarshalBinary If the BinaryEncoder is implemented correctly, this event should never happen.
CacheMiss denotes the key was not found in the cache.
CacheSetError denotes an error occurred with the cache or underlying storage (during sets).
CacheUnmarshalError denotes an error occurred in while calling BinaryEncoder.UnmarshalBinary If the BinaryEncoder is implemented correctly, this event should never happen.
CbDynamoDbStorage is tag for DynamoDB storage circuit breaker.
CbRedisStorage is tag for redis storage circuit breaker.

# Variables

ErrCacheMiss is returned when the cache does not contain the requested key.

# Structs

Client defines a cache instance.
DynamoDbStorage implements Storage It is strongly recommended that users customize the circuit breaker settings with a call similar to: hystrix.ConfigureCommand(cache.CbDynamoDbStorage, hystrix.CommandConfig{ Timeout: 1 * 1000, MaxConcurrentRequests: 1000, ErrorPercentThreshold: 50, }) .
LambdaError is the error type returned when the user fetch/build lambda failed.
MockStorage is an autogenerated mock type for the Storage type.
RedisStorage implements Storage It is strongly recommended that users customize the circuit breaker settings with a call similar to: hystrix.ConfigureCommand(cache.CbRedisStorage, hystrix.CommandConfig{ Timeout: 1 * 1000, MaxConcurrentRequests: 1000, ErrorPercentThreshold: 50, }) .

# Interfaces

API defines the main API for this package.
BinaryEncoder encodes/decodes the receiver to and from binary form.
Builder builds the data for a key.
Logger allows for logging errors in the asynchronous calls.
Metrics allows for instrumenting the cache for hit/miss and errorsgo:generate mockery -name Metrics -inpkg -testonly -case underscore.
Storage is an abstract definition of the underlying cache storagego:generate mockery -name Storage -inpkg -case underscore.

# Type aliases

BuilderFunc implements Builder as a function.
Event denote the cache event type.
LoggerFunc implements Logger as a function.
MetricsFunc implements Metrics as a function.