@@ -26,14 +26,16 @@ type Cache struct {
2626
2727// Item is an item stored in the cache.
2828type Item struct {
29- Object interface {}
29+ // Object is the item's value.
30+ Object interface {}
31+ // Expiration is the item's expiration time.
3032 Expiration int64
3133}
3234
3335type cache struct {
3436 // Items holds the elements in the cache.
3537 Items map [string ]Item
36- // Maximum number of items the cache can hold.
38+ // MaxItems is the maximum number of items the cache can hold.
3739 MaxItems int
3840 mu sync.RWMutex
3941 janitor * janitor
@@ -82,6 +84,9 @@ func (c *cache) Set(key string, value interface{}, expiration time.Duration) err
8284 return fmt .Errorf ("Cache is full" )
8385}
8486
87+ // Add an item to the cache, existing items will not be overwritten.
88+ // To overwrite existing items, use Set.
89+ // If the cache is full, Add will return an error.
8590func (c * cache ) Add (key string , value interface {}, expiration time.Duration ) error {
8691 c .mu .Lock ()
8792 _ , found := c .Items [key ]
@@ -100,6 +105,8 @@ func (c *cache) Add(key string, value interface{}, expiration time.Duration) err
100105 return fmt .Errorf ("Cache is full" )
101106}
102107
108+ // Get an item from the cache. Returns the item or nil, and a bool indicating
109+ // whether the key was found.
103110func (c * cache ) Get (key string ) (interface {}, bool ) {
104111 c .mu .RLock ()
105112 item , found := c .Items [key ]
@@ -117,18 +124,23 @@ func (c *cache) Get(key string) (interface{}, bool) {
117124 return item .Object , true
118125}
119126
127+ // Delete an item from the cache. Does nothing if the key is not in the cache.
120128func (c * cache ) Delete (key string ) {
121129 c .mu .Lock ()
122130 delete (c .Items , key )
123131 c .mu .Unlock ()
124132}
125133
134+ // Clear all items from the cache.
135+ // This reallocate the inderlying array holding the items,
136+ // so that the memory used by the items is reclaimed.
126137func (c * cache ) Clear () {
127138 c .mu .Lock ()
128139 c .Items = make (map [string ]Item )
129140 c .mu .Unlock ()
130141}
131142
143+ // HasExpired returns true if the item has expired.
132144func (c * cache ) HasExpired (key string ) bool {
133145 c .mu .RLock ()
134146 item , ok := c .Items [key ]
@@ -146,6 +158,8 @@ func (c *cache) HasExpired(key string) bool {
146158 return false
147159}
148160
161+ // SetExpiration sets the expiration for the given key.
162+ // Does nothing if the key is not in the cache.
149163func (c * cache ) SetExpiration (key string , expiration time.Duration ) {
150164 c .mu .Lock ()
151165 item , ok := c .Items [key ]
@@ -157,6 +171,9 @@ func (c *cache) SetExpiration(key string, expiration time.Duration) {
157171 c .mu .Unlock ()
158172}
159173
174+ // GetExpiration returns the expiration for the given key.
175+ // Returns zero if the key is not in the cache or the item
176+ // has already expired.
160177func (c * cache ) GetExpiration (key string ) time.Duration {
161178 c .mu .RLock ()
162179 item , ok := c .Items [key ]
@@ -174,6 +191,7 @@ func (c *cache) GetExpiration(key string) time.Duration {
174191 return time .Duration (item .Expiration - time .Now ().UnixNano ())
175192}
176193
194+ // DeleteExpired deletes all expired items from the cache.
177195func (c * cache ) DeleteExpired () {
178196 c .mu .Lock ()
179197 for k , v := range c .Items {
@@ -185,12 +203,12 @@ func (c *cache) DeleteExpired() {
185203}
186204
187205type janitor struct {
188- Interval time.Duration
206+ interval time.Duration
189207 stop chan bool
190208}
191209
192- func (j * janitor ) Run (c * cache ) {
193- ticker := time .NewTicker (j .Interval )
210+ func (j * janitor ) run (c * cache ) {
211+ ticker := time .NewTicker (j .interval )
194212 for {
195213 select {
196214 case <- ticker .C :
@@ -206,20 +224,21 @@ func stopJanitor(c *Cache) {
206224 c .janitor .stop <- true
207225}
208226
227+ // New creates a new cache with the given configuration.
209228func New (maxItems int , interval time.Duration ) * Cache {
210229 c := & cache {
211230 Items : make (map [string ]Item ),
212231 MaxItems : maxItems ,
213232 janitor : & janitor {
214- Interval : interval ,
233+ interval : interval ,
215234 stop : make (chan bool ),
216235 },
217236 }
218237
219238 C := & Cache {c }
220239
221240 if interval > 0 {
222- go c .janitor .Run (c )
241+ go c .janitor .run (c )
223242 runtime .SetFinalizer (C , stopJanitor )
224243 }
225244
0 commit comments