mirror of
https://github.com/astaxie/beego.git
synced 2025-06-11 12:40:40 +00:00
Minor grammar fixes
This commit is contained in:
10
pkg/cache/conv.go
vendored
10
pkg/cache/conv.go
vendored
@ -19,7 +19,7 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetString convert interface to string.
|
||||
// GetString converts interface to string.
|
||||
func GetString(v interface{}) string {
|
||||
switch result := v.(type) {
|
||||
case string:
|
||||
@ -34,7 +34,7 @@ func GetString(v interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetInt convert interface to int.
|
||||
// GetInt converts interface to int.
|
||||
func GetInt(v interface{}) int {
|
||||
switch result := v.(type) {
|
||||
case int:
|
||||
@ -52,7 +52,7 @@ func GetInt(v interface{}) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt64 convert interface to int64.
|
||||
// GetInt64 converts interface to int64.
|
||||
func GetInt64(v interface{}) int64 {
|
||||
switch result := v.(type) {
|
||||
case int:
|
||||
@ -71,7 +71,7 @@ func GetInt64(v interface{}) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetFloat64 convert interface to float64.
|
||||
// GetFloat64 converts interface to float64.
|
||||
func GetFloat64(v interface{}) float64 {
|
||||
switch result := v.(type) {
|
||||
case float64:
|
||||
@ -85,7 +85,7 @@ func GetFloat64(v interface{}) float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetBool convert interface to bool.
|
||||
// GetBool converts interface to bool.
|
||||
func GetBool(v interface{}) bool {
|
||||
switch result := v.(type) {
|
||||
case bool:
|
||||
|
47
pkg/cache/file.go
vendored
47
pkg/cache/file.go
vendored
@ -30,8 +30,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// FileCacheItem is basic unit of file cache adapter.
|
||||
// it contains data and expire time.
|
||||
// FileCacheItem is basic unit of file cache adapter which
|
||||
// contains data and expire time.
|
||||
type FileCacheItem struct {
|
||||
Data interface{}
|
||||
Lastaccess time.Time
|
||||
@ -54,15 +54,15 @@ type FileCache struct {
|
||||
EmbedExpiry int
|
||||
}
|
||||
|
||||
// NewFileCache Create new file cache with no config.
|
||||
// the level and expiry need set in method StartAndGC as config string.
|
||||
// NewFileCache cerates a new file cache with no config.
|
||||
// The level and expiry need to be set in the method StartAndGC as config string.
|
||||
func NewFileCache() Cache {
|
||||
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
|
||||
return &FileCache{}
|
||||
}
|
||||
|
||||
// StartAndGC will start and begin gc for file cache.
|
||||
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
|
||||
// StartAndGC starts gc for file cache.
|
||||
// config must be in the format {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
|
||||
func (fc *FileCache) StartAndGC(config string) error {
|
||||
|
||||
cfg := make(map[string]string)
|
||||
@ -91,14 +91,14 @@ func (fc *FileCache) StartAndGC(config string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Init will make new dir for file cache if not exist.
|
||||
// Init makes new a dir for file cache if it does not already exist
|
||||
func (fc *FileCache) Init() {
|
||||
if ok, _ := exists(fc.CachePath); !ok { // todo : error handle
|
||||
_ = os.MkdirAll(fc.CachePath, os.ModePerm) // todo : error handle
|
||||
}
|
||||
}
|
||||
|
||||
// get cached file name. it's md5 encoded.
|
||||
// getCachedFilename returns an md5 encoded file name.
|
||||
func (fc *FileCache) getCacheFileName(key string) string {
|
||||
m := md5.New()
|
||||
io.WriteString(m, key)
|
||||
@ -119,7 +119,7 @@ func (fc *FileCache) getCacheFileName(key string) string {
|
||||
}
|
||||
|
||||
// Get value from file cache.
|
||||
// if non-exist or expired, return empty string.
|
||||
// if nonexistent or expired return an empty string.
|
||||
func (fc *FileCache) Get(key string) interface{} {
|
||||
fileData, err := FileGetContents(fc.getCacheFileName(key))
|
||||
if err != nil {
|
||||
@ -134,7 +134,7 @@ func (fc *FileCache) Get(key string) interface{} {
|
||||
}
|
||||
|
||||
// GetMulti gets values from file cache.
|
||||
// if non-exist or expired, return empty string.
|
||||
// if nonexistent or expired return an empty string.
|
||||
func (fc *FileCache) GetMulti(keys []string) []interface{} {
|
||||
var rc []interface{}
|
||||
for _, key := range keys {
|
||||
@ -144,7 +144,7 @@ func (fc *FileCache) GetMulti(keys []string) []interface{} {
|
||||
}
|
||||
|
||||
// Put value into file cache.
|
||||
// timeout means how long to keep this file, unit of ms.
|
||||
// timeout: how long this file should be kept in ms
|
||||
// if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.
|
||||
func (fc *FileCache) Put(key string, val interface{}, timeout time.Duration) error {
|
||||
gob.Register(val)
|
||||
@ -172,8 +172,8 @@ func (fc *FileCache) Delete(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Incr will increase cached int value.
|
||||
// fc value is saving forever unless Delete.
|
||||
// Incr increases cached int value.
|
||||
// fc value is saved forever unless deleted.
|
||||
func (fc *FileCache) Incr(key string) error {
|
||||
data := fc.Get(key)
|
||||
var incr int
|
||||
@ -186,7 +186,7 @@ func (fc *FileCache) Incr(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decr will decrease cached int value.
|
||||
// Decr decreases cached int value.
|
||||
func (fc *FileCache) Decr(key string) error {
|
||||
data := fc.Get(key)
|
||||
var decr int
|
||||
@ -199,19 +199,18 @@ func (fc *FileCache) Decr(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsExist check value is exist.
|
||||
// IsExist checks if value exists.
|
||||
func (fc *FileCache) IsExist(key string) bool {
|
||||
ret, _ := exists(fc.getCacheFileName(key))
|
||||
return ret
|
||||
}
|
||||
|
||||
// ClearAll will clean cached files.
|
||||
// not implemented.
|
||||
// ClearAll cleans cached files (not implemented)
|
||||
func (fc *FileCache) ClearAll() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// check file exist.
|
||||
// Check if a file exists
|
||||
func exists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
@ -223,19 +222,19 @@ func exists(path string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// FileGetContents Get bytes to file.
|
||||
// if non-exist, create this file.
|
||||
// FileGetContents Reads bytes from a file.
|
||||
// if non-existent, create this file.
|
||||
func FileGetContents(filename string) (data []byte, e error) {
|
||||
return ioutil.ReadFile(filename)
|
||||
}
|
||||
|
||||
// FilePutContents Put bytes to file.
|
||||
// if non-exist, create this file.
|
||||
// FilePutContents puts bytes into a file.
|
||||
// if non-existent, create this file.
|
||||
func FilePutContents(filename string, content []byte) error {
|
||||
return ioutil.WriteFile(filename, content, os.ModePerm)
|
||||
}
|
||||
|
||||
// GobEncode Gob encodes file cache item.
|
||||
// GobEncode Gob encodes a file cache item.
|
||||
func GobEncode(data interface{}) ([]byte, error) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
enc := gob.NewEncoder(buf)
|
||||
@ -246,7 +245,7 @@ func GobEncode(data interface{}) ([]byte, error) {
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
// GobDecode Gob decodes file cache item.
|
||||
// GobDecode Gob decodes a file cache item.
|
||||
func GobDecode(data []byte, to *FileCacheItem) error {
|
||||
buf := bytes.NewBuffer(data)
|
||||
dec := gob.NewDecoder(buf)
|
||||
|
34
pkg/cache/memory.go
vendored
34
pkg/cache/memory.go
vendored
@ -22,11 +22,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultEvery means the clock time of recycling the expired cache items in memory.
|
||||
// Recycle the expired cache items in memory (in seconds)
|
||||
DefaultEvery = 60 // 1 minute
|
||||
)
|
||||
|
||||
// MemoryItem store memory cache item.
|
||||
// MemoryItem stores memory cache item.
|
||||
type MemoryItem struct {
|
||||
val interface{}
|
||||
createdTime time.Time
|
||||
@ -41,8 +41,8 @@ func (mi *MemoryItem) isExpire() bool {
|
||||
return time.Now().Sub(mi.createdTime) > mi.lifespan
|
||||
}
|
||||
|
||||
// MemoryCache is Memory cache adapter.
|
||||
// it contains a RW locker for safe map storage.
|
||||
// MemoryCache is a memory cache adapter.
|
||||
// Contains a RW locker for safe map storage.
|
||||
type MemoryCache struct {
|
||||
sync.RWMutex
|
||||
dur time.Duration
|
||||
@ -56,8 +56,8 @@ func NewMemoryCache() Cache {
|
||||
return &cache
|
||||
}
|
||||
|
||||
// Get cache from memory.
|
||||
// if non-existed or expired, return nil.
|
||||
// Get returns cache from memory.
|
||||
// If non-existent or expired, return nil.
|
||||
func (bc *MemoryCache) Get(name string) interface{} {
|
||||
bc.RLock()
|
||||
defer bc.RUnlock()
|
||||
@ -71,7 +71,7 @@ func (bc *MemoryCache) Get(name string) interface{} {
|
||||
}
|
||||
|
||||
// GetMulti gets caches from memory.
|
||||
// if non-existed or expired, return nil.
|
||||
// If non-existent or expired, return nil.
|
||||
func (bc *MemoryCache) GetMulti(names []string) []interface{} {
|
||||
var rc []interface{}
|
||||
for _, name := range names {
|
||||
@ -80,8 +80,8 @@ func (bc *MemoryCache) GetMulti(names []string) []interface{} {
|
||||
return rc
|
||||
}
|
||||
|
||||
// Put cache to memory.
|
||||
// if lifespan is 0, it will be forever till restart.
|
||||
// Put puts cache into memory.
|
||||
// If lifespan is 0, it will never overwrite this value
|
||||
func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
@ -107,8 +107,8 @@ func (bc *MemoryCache) Delete(name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Incr increase cache counter in memory.
|
||||
// it supports int,int32,int64,uint,uint32,uint64.
|
||||
// Incr increases cache counter in memory.
|
||||
// Supports int,int32,int64,uint,uint32,uint64.
|
||||
func (bc *MemoryCache) Incr(key string) error {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
@ -135,7 +135,7 @@ func (bc *MemoryCache) Incr(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decr decrease counter in memory.
|
||||
// Decr decreases counter in memory.
|
||||
func (bc *MemoryCache) Decr(key string) error {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
@ -174,7 +174,7 @@ func (bc *MemoryCache) Decr(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsExist check cache exist in memory.
|
||||
// IsExist checks if cache exists in memory.
|
||||
func (bc *MemoryCache) IsExist(name string) bool {
|
||||
bc.RLock()
|
||||
defer bc.RUnlock()
|
||||
@ -184,7 +184,7 @@ func (bc *MemoryCache) IsExist(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ClearAll will delete all cache in memory.
|
||||
// ClearAll deletes all cache in memory.
|
||||
func (bc *MemoryCache) ClearAll() error {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
@ -192,7 +192,7 @@ func (bc *MemoryCache) ClearAll() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartAndGC start memory cache. it will check expiration in every clock time.
|
||||
// StartAndGC starts memory cache. Checks expiration in every clock time.
|
||||
func (bc *MemoryCache) StartAndGC(config string) error {
|
||||
var cf map[string]int
|
||||
json.Unmarshal([]byte(config), &cf)
|
||||
@ -230,7 +230,7 @@ func (bc *MemoryCache) vacuum() {
|
||||
}
|
||||
}
|
||||
|
||||
// expiredKeys returns key list which are expired.
|
||||
// expiredKeys returns keys list which are expired.
|
||||
func (bc *MemoryCache) expiredKeys() (keys []string) {
|
||||
bc.RLock()
|
||||
defer bc.RUnlock()
|
||||
@ -242,7 +242,7 @@ func (bc *MemoryCache) expiredKeys() (keys []string) {
|
||||
return
|
||||
}
|
||||
|
||||
// clearItems removes all the items which key in keys.
|
||||
// ClearItems removes all items who's key is in keys
|
||||
func (bc *MemoryCache) clearItems(keys []string) {
|
||||
bc.Lock()
|
||||
defer bc.Unlock()
|
||||
|
Reference in New Issue
Block a user