Skip to content

Commit d7aaf2e

Browse files
committed
golint cache package
1 parent 62e528c commit d7aaf2e

7 files changed

Lines changed: 106 additions & 105 deletions

File tree

cache/cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Package cache provide a Cache interface and some implemetn engine
1516
// Usage:
1617
//
1718
// import(
@@ -80,7 +81,7 @@ func Register(name string, adapter Cache) {
8081
adapters[name] = adapter
8182
}
8283

83-
// Create a new cache driver by adapter name and config string.
84+
// NewCache Create a new cache driver by adapter name and config string.
8485
// config need to be correct JSON as string: {"interval":360}.
8586
// it will start gc automatically.
8687
func NewCache(adapterName, config string) (adapter Cache, err error) {

cache/conv.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"strconv"
2020
)
2121

22-
// convert interface to string.
22+
// GetString convert interface to string.
2323
func GetString(v interface{}) string {
2424
switch result := v.(type) {
2525
case string:
@@ -34,7 +34,7 @@ func GetString(v interface{}) string {
3434
return ""
3535
}
3636

37-
// convert interface to int.
37+
// GetInt convert interface to int.
3838
func GetInt(v interface{}) int {
3939
switch result := v.(type) {
4040
case int:
@@ -52,7 +52,7 @@ func GetInt(v interface{}) int {
5252
return 0
5353
}
5454

55-
// convert interface to int64.
55+
// GetInt64 convert interface to int64.
5656
func GetInt64(v interface{}) int64 {
5757
switch result := v.(type) {
5858
case int:
@@ -71,7 +71,7 @@ func GetInt64(v interface{}) int64 {
7171
return 0
7272
}
7373

74-
// convert interface to float64.
74+
// GetFloat64 convert interface to float64.
7575
func GetFloat64(v interface{}) float64 {
7676
switch result := v.(type) {
7777
case float64:
@@ -85,7 +85,7 @@ func GetFloat64(v interface{}) float64 {
8585
return 0
8686
}
8787

88-
// convert interface to bool.
88+
// GetBool convert interface to bool.
8989
func GetBool(v interface{}) bool {
9090
switch result := v.(type) {
9191
case bool:
@@ -99,7 +99,7 @@ func GetBool(v interface{}) bool {
9999
return false
100100
}
101101

102-
// convert interface to byte slice.
102+
// getByteArray convert interface to byte slice.
103103
func getByteArray(v interface{}) []byte {
104104
switch result := v.(type) {
105105
case []byte:

cache/conv_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func TestGetString(t *testing.T) {
2727
if "test2" != GetString(t2) {
2828
t.Error("get string from byte array error")
2929
}
30-
var t3 int = 1
30+
var t3 = 1
3131
if "1" != GetString(t3) {
3232
t.Error("get string from int error")
3333
}
3434
var t4 int64 = 1
3535
if "1" != GetString(t4) {
3636
t.Error("get string from int64 error")
3737
}
38-
var t5 float64 = 1.1
38+
var t5 = 1.1
3939
if "1.1" != GetString(t5) {
4040
t.Error("get string from float64 error")
4141
}
@@ -46,7 +46,7 @@ func TestGetString(t *testing.T) {
4646
}
4747

4848
func TestGetInt(t *testing.T) {
49-
var t1 int = 1
49+
var t1 = 1
5050
if 1 != GetInt(t1) {
5151
t.Error("get int from int error")
5252
}
@@ -69,7 +69,7 @@ func TestGetInt(t *testing.T) {
6969

7070
func TestGetInt64(t *testing.T) {
7171
var i int64 = 1
72-
var t1 int = 1
72+
var t1 = 1
7373
if i != GetInt64(t1) {
7474
t.Error("get int64 from int error")
7575
}
@@ -91,12 +91,12 @@ func TestGetInt64(t *testing.T) {
9191
}
9292

9393
func TestGetFloat64(t *testing.T) {
94-
var f float64 = 1.11
94+
var f = 1.11
9595
var t1 float32 = 1.11
9696
if f != GetFloat64(t1) {
9797
t.Error("get float64 from float32 error")
9898
}
99-
var t2 float64 = 1.11
99+
var t2 = 1.11
100100
if f != GetFloat64(t2) {
101101
t.Error("get float64 from float64 error")
102102
}
@@ -106,7 +106,7 @@ func TestGetFloat64(t *testing.T) {
106106
}
107107

108108
var f2 float64 = 1
109-
var t4 int = 1
109+
var t4 = 1
110110
if f2 != GetFloat64(t4) {
111111
t.Error("get float64 from int error")
112112
}

cache/file.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ type FileCacheItem struct {
4141
Expired int64
4242
}
4343

44+
// FileCache Config
4445
var (
45-
FileCachePath string = "cache" // cache directory
46-
FileCacheFileSuffix string = ".bin" // cache file suffix
47-
FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files.
48-
FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever.
46+
FileCachePath = "cache" // cache directory
47+
FileCacheFileSuffix = ".bin" // cache file suffix
48+
FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files.
49+
FileCacheEmbedExpiry int64 // cache expire time, default is no expire forever.
4950
)
5051

5152
// FileCache is cache adapter for file storage.
@@ -56,14 +57,14 @@ type FileCache struct {
5657
EmbedExpiry int
5758
}
5859

59-
// Create new file cache with no config.
60+
// NewFileCache Create new file cache with no config.
6061
// the level and expiry need set in method StartAndGC as config string.
6162
func NewFileCache() *FileCache {
6263
// return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix}
6364
return &FileCache{}
6465
}
6566

66-
// Start and begin gc for file cache.
67+
// StartAndGC will start and begin gc for file cache.
6768
// the config need to be like {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":2,"EmbedExpiry":0}
6869
func (fc *FileCache) StartAndGC(config string) error {
6970

@@ -120,12 +121,12 @@ func (fc *FileCache) getCacheFileName(key string) string {
120121
// Get value from file cache.
121122
// if non-exist or expired, return empty string.
122123
func (fc *FileCache) Get(key string) interface{} {
123-
fileData, err := File_get_contents(fc.getCacheFileName(key))
124+
fileData, err := FileGetContents(fc.getCacheFileName(key))
124125
if err != nil {
125126
return ""
126127
}
127128
var to FileCacheItem
128-
Gob_decode(fileData, &to)
129+
GobDecode(fileData, &to)
129130
if to.Expired < time.Now().Unix() {
130131
return ""
131132
}
@@ -155,11 +156,11 @@ func (fc *FileCache) Put(key string, val interface{}, timeout int64) error {
155156
item.Expired = time.Now().Unix() + timeout
156157
}
157158
item.Lastaccess = time.Now().Unix()
158-
data, err := Gob_encode(item)
159+
data, err := GobEncode(item)
159160
if err != nil {
160161
return err
161162
}
162-
return File_put_contents(fc.getCacheFileName(key), data)
163+
return FilePutContents(fc.getCacheFileName(key), data)
163164
}
164165

165166
// Delete file cache value.
@@ -171,7 +172,7 @@ func (fc *FileCache) Delete(key string) error {
171172
return nil
172173
}
173174

174-
// Increase cached int value.
175+
// Incr will increase cached int value.
175176
// fc value is saving forever unless Delete.
176177
func (fc *FileCache) Incr(key string) error {
177178
data := fc.Get(key)
@@ -185,7 +186,7 @@ func (fc *FileCache) Incr(key string) error {
185186
return nil
186187
}
187188

188-
// Decrease cached int value.
189+
// Decr will decrease cached int value.
189190
func (fc *FileCache) Decr(key string) error {
190191
data := fc.Get(key)
191192
var decr int
@@ -198,13 +199,13 @@ func (fc *FileCache) Decr(key string) error {
198199
return nil
199200
}
200201

201-
// Check value is exist.
202+
// IsExist check value is exist.
202203
func (fc *FileCache) IsExist(key string) bool {
203204
ret, _ := exists(fc.getCacheFileName(key))
204205
return ret
205206
}
206207

207-
// Clean cached files.
208+
// ClearAll will clean cached files.
208209
// not implemented.
209210
func (fc *FileCache) ClearAll() error {
210211
return nil
@@ -222,9 +223,9 @@ func exists(path string) (bool, error) {
222223
return false, err
223224
}
224225

225-
// Get bytes to file.
226+
// FileGetContents Get bytes to file.
226227
// if non-exist, create this file.
227-
func File_get_contents(filename string) (data []byte, e error) {
228+
func FileGetContents(filename string) (data []byte, e error) {
228229
f, e := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
229230
if e != nil {
230231
return
@@ -242,9 +243,9 @@ func File_get_contents(filename string) (data []byte, e error) {
242243
return
243244
}
244245

245-
// Put bytes to file.
246+
// FilePutContents Put bytes to file.
246247
// if non-exist, create this file.
247-
func File_put_contents(filename string, content []byte) error {
248+
func FilePutContents(filename string, content []byte) error {
248249
fp, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
249250
if err != nil {
250251
return err
@@ -254,8 +255,8 @@ func File_put_contents(filename string, content []byte) error {
254255
return err
255256
}
256257

257-
// Gob encodes file cache item.
258-
func Gob_encode(data interface{}) ([]byte, error) {
258+
// GobEncode Gob encodes file cache item.
259+
func GobEncode(data interface{}) ([]byte, error) {
259260
buf := bytes.NewBuffer(nil)
260261
enc := gob.NewEncoder(buf)
261262
err := enc.Encode(data)
@@ -265,8 +266,8 @@ func Gob_encode(data interface{}) ([]byte, error) {
265266
return buf.Bytes(), err
266267
}
267268

268-
// Gob decodes file cache item.
269-
func Gob_decode(data []byte, to *FileCacheItem) error {
269+
// GobDecode Gob decodes file cache item.
270+
func GobDecode(data []byte, to *FileCacheItem) error {
270271
buf := bytes.NewBuffer(data)
271272
dec := gob.NewDecoder(buf)
272273
return dec.Decode(&to)

0 commit comments

Comments
 (0)