Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "fmt" _ "github.com/lib/pq" cache "github.com/takuoki/gocodecache" ) func main() { ctx := context.Background() // -- How to initialize -- if err := cache.InitializeGlobalCache(ctx, cache.YAMLSource("./sample/codes.yaml", "CODES"), 2); err != nil { // handle error } // -- How to get value -- accType1Str, err := cache.GetValue(ctx, "account_type", "1") if err != nil { // handle error } fmt.Println(accType1Str) fmt.Println(cache.MustGetValue(ctx, "visibility_level", "2")) }
Output: Anonymous account Public
Index ¶
- Constants
- Variables
- func GetValue(ctx context.Context, keys ...string) (string, error)
- func InitializeGlobalCache(ctx context.Context, ds Datasource, keyLength int, opts ...InitializeOption) error
- func MustGetValue(ctx context.Context, keys ...string) string
- func Reload(ctx context.Context) error
- type Cache
- type CodeLists
- type Codes
- type Datasource
- type InitializeOption
Examples ¶
Constants ¶
View Source
const MaxKeyLength = 5
Variables ¶
View Source
var ( ErrNotInitialized = errors.New("cache is not initialized") ErrCodeNotFound = errors.New("code not found") )
Functions ¶
func InitializeGlobalCache ¶
func InitializeGlobalCache(ctx context.Context, ds Datasource, keyLength int, opts ...InitializeOption) error
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Example ¶
package main import ( "context" "fmt" _ "github.com/lib/pq" cache "github.com/takuoki/gocodecache" ) func main() { ctx := context.Background() // -- How to initialize -- c, err := cache.New(ctx, cache.YAMLSource("./sample/codes_lang.yaml", "CODES"), 3) if err != nil { // handle error } // -- How to get value -- accType1jaStr, err := c.GetValue(ctx, "account_type", "1", "ja-JP") if err != nil { // handle error } fmt.Println(accType1jaStr) fmt.Println(c.MustGetValue(ctx, "visibility_level", "2", "en-US")) }
Output: 匿名アカウント Public
func New ¶
func New(ctx context.Context, ds Datasource, keyLength int, opts ...InitializeOption) (*Cache, error)
func (*Cache) MustGetValue ¶
type CodeLists ¶ added in v0.2.0
type CodeLists map[[MaxKeyLength - 1]string][]string
type Codes ¶ added in v0.2.0
type Codes map[[MaxKeyLength]string]string
type Datasource ¶
type Datasource interface { ReadAll(ctx context.Context, keyLength int) (Codes, error) ReadFirstKeys(ctx context.Context, keyLength int, firstKeys map[string]struct{}) (Codes, error) }
func RawSource ¶
func RawSource(data Codes) Datasource
func RdbSource ¶
func RdbSource(db *sql.DB, tableName string, keyColumnNames []string, valueColumnName string, ) Datasource
Example ¶
ctx := context.Background() // -- How to initialize -- db, err := sql.Open( "postgres", fmt.Sprintf( "host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", "localhost", "5432", "root", "root", "postgres", "disable", ), ) if err != nil { // handle error } defer db.Close() for i := 0; ; i++ { if i >= dbPingRetryLimit { // handle error } if err = db.Ping(); err == nil { break } time.Sleep(dbPingRetryInterval) } c, err := cache.New(ctx, cache.RdbSource(db, "codes", []string{"key1", "key2"}, "value"), 2) if err != nil { // handle error } // -- How to get value -- accType1jaStr, err := c.GetValue(ctx, "account_type", "1") if err != nil { // handle error } fmt.Println(accType1jaStr) fmt.Println(c.MustGetValue(ctx, "visibility_level", "2"))
Output: Anonymous account Public
func YAMLSource ¶
func YAMLSource(filepath, rootKey string) Datasource
type InitializeOption ¶ added in v0.2.0
type InitializeOption interface {
// contains filtered or unexported methods
}
func WithLoadFirstKeys ¶ added in v0.2.0
func WithLoadFirstKeys(firstKeys ...string) InitializeOption
Click to show internal directories.
Click to hide internal directories.