Purge stale object cache entry (#2770)

This commit is contained in:
Krishnan Parthasarathi
2016-09-23 19:55:28 -07:00
committed by Harshavardhana
parent 27e474b3d2
commit 669783f875
3 changed files with 38 additions and 6 deletions

View File

@@ -163,7 +163,9 @@ func (c *Cache) Create(key string, size int64) (w io.WriteCloser, err error) {
// Open - open the in-memory file, returns an in memory read seeker.
// returns an error ErrNotFoundInCache, if the key does not exist.
func (c *Cache) Open(key string) (io.ReadSeeker, error) {
// Returns ErrKeyNotFoundInCache if entry's lastAccessedTime is older
// than objModTime.
func (c *Cache) Open(key string, objModTime time.Time) (io.ReadSeeker, error) {
// Entry exists, return the readable buffer.
c.mutex.Lock()
defer c.mutex.Unlock()
@@ -171,6 +173,11 @@ func (c *Cache) Open(key string) (io.ReadSeeker, error) {
if !ok {
return nil, ErrKeyNotFoundInCache
}
// Check if buf is recent copy of the object on disk.
if buf.lastAccessed.Before(objModTime) {
c.delete(key)
return nil, ErrKeyNotFoundInCache
}
buf.lastAccessed = time.Now().UTC()
return bytes.NewReader(buf.value), nil
}