@@ -41,11 +41,12 @@ type FileCacheItem struct {
4141 Expired int64
4242}
4343
44+ // FileCache Config
4445var (
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.
6162func 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}
6869func (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.
122123func (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.
176177func (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.
189190func (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.
202203func (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.
209210func (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