forked from muesli/cache2go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheitem.go
More file actions
108 lines (93 loc) · 2.51 KB
/
Copy pathcacheitem.go
File metadata and controls
108 lines (93 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Simple caching library with expiration capabilities
* Copyright (c) 2013, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE.txt
*/
package cache2go
import (
"sync"
"time"
)
// Structure of an item in the cache.
// Parameter data contains the user-set value in the cache.
type CacheItem struct {
sync.RWMutex
// The item's key.
key interface{}
// The item's data.
data interface{}
// How long will the item live in the cache when not being accessed/kept alive.
lifeSpan time.Duration
// Creation timestamp.
createdOn time.Time
// Last access timestamp.
accessedOn time.Time
// How often the item was accessed.
accessCount int64
// Callback method triggered right before removing the item from the cache
aboutToExpire func(key interface{})
}
// Returns a newly created CacheItem.
// Parameter key is the item's cache-key.
// Parameter lifeSpan determines after which time period without an access the item
// will get removed from the cache.
// Parameter data is the item's value.
func CreateCacheItem(key interface{}, lifeSpan time.Duration, data interface{}) CacheItem {
t := time.Now()
return CacheItem{
key: key,
lifeSpan: lifeSpan,
createdOn: t,
accessedOn: t,
accessCount: 0,
aboutToExpire: nil,
data: data,
}
}
// Mark item to be kept for another expireDuration period.
func (item *CacheItem) KeepAlive() {
item.Lock()
defer item.Unlock()
item.accessedOn = time.Now()
item.accessCount++
}
// Returns this item's expiration duration.
func (item *CacheItem) LifeSpan() time.Duration {
// immutable
return item.lifeSpan
}
// Returns when this item was last accessed.
func (item *CacheItem) AccessedOn() time.Time {
item.RLock()
defer item.RUnlock()
return item.accessedOn
}
// Returns when this item was added to the cache.
func (item *CacheItem) CreatedOn() time.Time {
// immutable
return item.createdOn
}
// Returns how often this item has been accessed.
func (item *CacheItem) AccessCount() int64 {
item.RLock()
defer item.RUnlock()
return item.accessCount
}
// Returns the key of this cached item.
func (item *CacheItem) Key() interface{} {
// immutable
return item.key
}
// Returns the value of this cached item.
func (item *CacheItem) Data() interface{} {
// immutable
return item.data
}
// Configures a callback, which will be called right before the item
// is about to be removed from the cache.
func (item *CacheItem) SetAboutToExpireCallback(f func(interface{})) {
item.Lock()
defer item.Unlock()
item.aboutToExpire = f
}