Skip to content

Commit

Permalink
Add Expired for simplecache.
Browse files Browse the repository at this point in the history
  • Loading branch information
toomore committed Jan 2, 2016
1 parent c24b2c1 commit 4cfe51b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions flickr/flickr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"net/url"
"time"

"github.com/toomore/lazyflickrgo/simplecache"
"github.com/toomore/lazyflickrgo/utils"
Expand All @@ -22,8 +23,8 @@ var tempDir string
var cache *simplecache.SimlpleCache

func init() {
cache = simplecache.NewSimpleCache("", "lzf")
log.Println("Temp Dir: ", cache.Dir, cache.Folder)
cache = simplecache.NewSimpleCache("", "lzf", 24*time.Hour)
log.Printf("Temp Dir: %s/%s, Expired: %s\n", cache.Dir, cache.Folder, cache.Expired)
}

// NewFlickr is to new a request.
Expand Down
9 changes: 8 additions & 1 deletion simplecache/simplecache.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package simplecache

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"time"
)

// SimlpleCache struct
type SimlpleCache struct {
Folder string
Dir string
Expired time.Duration
fullpath string
}

// NewSimpleCache new a SimlpleCache
func NewSimpleCache(Dir, Folder string) *SimlpleCache {
func NewSimpleCache(Dir, Folder string, Expired time.Duration) *SimlpleCache {
if Dir == "" {
Dir = getOSRamdiskPath()
}
Expand All @@ -30,6 +33,7 @@ func NewSimpleCache(Dir, Folder string) *SimlpleCache {
return &SimlpleCache{
Dir: Dir,
Folder: Folder,
Expired: Expired,
fullpath: fullpath,
}
}
Expand All @@ -39,6 +43,9 @@ func (s *SimlpleCache) Get(name string) ([]byte, error) {
var err error
if file, err := os.Open(filepath.Join(s.fullpath, name)); err == nil {
defer file.Close()
if stat, _ := file.Stat(); time.Now().Sub(stat.ModTime()) > s.Expired {
return nil, errors.New("Cache expired.")
}
return ioutil.ReadAll(file)
}
return nil, err
Expand Down

0 comments on commit 4cfe51b

Please sign in to comment.