gocodecache
An in-memory cache library for code master in Golang.
Installation
go get github.com/takuoki/gocodecache
Get started
Create YAML file
# ./sample/codes.yaml
VERSION: 0.1.0
CODES:
account_type:
1: Anonymous account
2: General account
3: Administrator account
visibility_level:
1: Private
2: Public
Code
package main
import (
"context"
"fmt"
cache "github.com/takuoki/gocodecache"
)
func main() {
ctx := context.Background()
if err := cache.InitializeGlobalCache(ctx, cache.YAMLSource("./sample/codes.yaml", "CODES"), 2); err != nil {
// handle error
}
accType1Str, err := cache.GetValue(ctx, "account_type", "1")
if err != nil {
// handle error
}
vLevel := cache.MustGetValue(ctx, "visibility_level", "2")
}
Datasource
Raw
Define a datasource as Golang code.
datasource := cache.RawSource(map[[cache.MaxKeyLength]string]string{
{"account_type", "1"}: "Anonymous account",
{"account_type", "2"}: "General account",
{"account_type", "3"}: "Administrator account",
{"visibility_level", "1"}: "Private",
{"visibility_level", "2"}: "Public",
})
YAML
datasource := cache.YAMLSource("./sample/codes.yaml", "CODES")
VERSION: 0.1.0
CODES:
account_type:
1: Anonymous account
2: General account
3: Administrator account
visibility_level:
1: Private
2: Public
PostgreSQL
datasource := cache.PostgresSource(db, "codes", [cache.MaxKeyLength]string{"key1", "key2"}, "value")
key1 | key2 | value |
---|---|---|
account_type | 1 | Anonymous account |
account_type | 2 | General account |
account_type | 3 | Administrator account |
visibility_level | 1 | Private |
visibility_level | 2 | Public |
Internationalization (I18n)
I18n can be supported by adding language codes to the keys.
# ./sample/codes_lang.yaml
VERSION: 0.1.0
CODES:
account_type:
1:
en-US: Anonymous account
ja-JP: 匿名アカウント
2:
en-US: General account
ja-JP: 一般アカウント
3:
en-US: Administrator account
ja-JP: 管理者アカウント
visibility_level:
1:
en-US: Private
ja-JP: 非公開
2:
en-US: Public
ja-JP: 公開