Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"file.go",
"memcache.go",
"store.go",
],
importpath = "go-common/library/net/http/blademaster/middleware/cache/store",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/memcache:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,65 @@
package store
import (
"context"
"io/ioutil"
"os"
"path"
"go-common/library/log"
"github.com/pkg/errors"
)
// FileConfig config of File.
type FileConfig struct {
RootDir string
}
// File is a degrade file service.
type File struct {
c *FileConfig
}
var _ Store = &File{}
// NewFile new a file degrade service.
func NewFile(fc *FileConfig) *File {
if fc == nil {
panic(errors.New("file config is nil"))
}
fs := &File{c: fc}
if err := os.MkdirAll(fs.c.RootDir, 0755); err != nil {
panic(errors.Wrapf(err, "dir: %s", fs.c.RootDir))
}
return fs
}
// Set save the result of location to file.
// expire is not implemented in file storage.
func (fs *File) Set(ctx context.Context, key string, bs []byte, _ int32) (err error) {
file := path.Join(fs.c.RootDir, key)
tmp := file + ".tmp"
if err = ioutil.WriteFile(tmp, bs, 0644); err != nil {
log.Error("ioutil.WriteFile(%s, bs, 0644): error(%v)", tmp, err)
err = errors.Wrapf(err, "key: %s", key)
return
}
if err = os.Rename(tmp, file); err != nil {
log.Error("os.Rename(%s, %s): error(%v)", tmp, file, err)
err = errors.Wrapf(err, "key: %s", key)
return
}
return
}
// Get get result from file by locaiton+params.
func (fs *File) Get(ctx context.Context, key string) (bs []byte, err error) {
p := path.Join(fs.c.RootDir, key)
if bs, err = ioutil.ReadFile(p); err != nil {
log.Error("ioutil.ReadFile(%s): error(%v)", p, err)
err = errors.Wrapf(err, "key: %s", key)
return
}
return
}

View File

@@ -0,0 +1,54 @@
package store
import (
"context"
"go-common/library/cache/memcache"
"go-common/library/log"
)
// Memcache represents the cache with memcached persistence
type Memcache struct {
pool *memcache.Pool
}
// NewMemcache new a memcache store.
func NewMemcache(c *memcache.Config) *Memcache {
if c == nil {
panic("cache config is nil")
}
return &Memcache{
pool: memcache.NewPool(c),
}
}
// Set save the result to memcache store.
func (ms *Memcache) Set(ctx context.Context, key string, value []byte, expire int32) (err error) {
item := &memcache.Item{
Key: key,
Value: value,
Expiration: expire,
}
conn := ms.pool.Get(ctx)
defer conn.Close()
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s) error(%v)", key, err)
}
return
}
// Get get result from mc by locaiton+params.
func (ms *Memcache) Get(ctx context.Context, key string) ([]byte, error) {
conn := ms.pool.Get(ctx)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
//ignore not found error
return nil, nil
}
log.Error("conn.Get(%s) error(%v)", key, err)
return nil, err
}
return r.Value, nil
}

View File

@@ -0,0 +1,15 @@
package store
import (
"context"
)
// Store is the interface of a cache backend
type Store interface {
// Get retrieves an item from the cache. Returns the item or nil, and a bool indicating
// whether the key was found.
Get(ctx context.Context, key string) ([]byte, error)
// Set sets an item to the cache, replacing any existing item.
Set(ctx context.Context, key string, value []byte, expire int32) error
}