Commit Graph

89 Commits

Author SHA1 Message Date
Jaime Martinez 38560f59f4
Delete expired items before setting
Check if an item exists and it has expired before setting so that
onEvicted is actually called.

Fixes #48.

Add test and split func

Use nanoseconds instead

Call onEvicted if item existed before setting

Call onEvicted if item.Expired
2020-10-15 10:57:29 +11:00
Vivian Mathews 0640633ccc Fix race condition
- the gc finalize for an object races with the janitor.Run goroutine
- because the janitor.stop channel is created in the Run() goroutine this leads
  to a data race.
- fix by creating the channel when the janitor is created
2017-07-21 14:56:50 -04:00
Alex Edwards 8c11fe2df0 Add GetWithExpiration 2016-12-08 14:50:49 +01:00
Patrick Mylund Nielsen e7a9def80f Add SetDefault() for setting with the default expiration 2016-11-25 18:48:19 -05:00
Patrick Mylund Nielsen 9e6d9117e7 Add 'inlining of expired' note to Items() 2016-11-25 13:57:39 -05:00
Patrick Mylund Nielsen a2d8b56f0c Make Items() return a copy rather than an unsynchronized reference to the underlying items map 2016-11-25 13:56:11 -05:00
Patrick Mylund Nielsen 5849ccb308 remove mu.RUnlock call from get 2016-01-08 15:02:42 -05:00
Patrick Mylund Nielsen d461c5d2dd 'Inline' set in Set, and do time checks before the lock 2015-12-01 11:08:43 -05:00
Patrick Mylund Nielsen 76f1250a65 Make OnEvicted() a little faster 2015-11-30 16:18:49 -05:00
Patrick Mylund Nielsen afadf13f9f Back to UnixNano(), syscall dependency isn't worth a few nanoseconds better performance 2015-11-30 15:12:19 -05:00
Patrick Mylund Nielsen 2f0c74ebb8 Use intermediary timevals 2015-11-30 15:02:02 -05:00
Patrick Mylund Nielsen 01842a547c Use timevals 2015-11-30 14:47:22 -05:00
Patrick Mylund Nielsen 1924ec3baf Remove expired() since it's no longer used (because of the inlining) 2015-11-30 14:14:52 -05:00
Patrick Mylund Nielsen 8084bd02b5 Inline expiration checks manually for performance 2015-11-30 14:12:45 -05:00
Patrick Mylund Nielsen eb4f9f6b2f Use UnixNano int64s instead of Time 2015-11-30 13:54:01 -05:00
Patrick Mylund Nielsen 31c7be0bed 'Inline' Get and Expired 2015-11-30 13:50:17 -05:00
Patrick Mylund Nielsen 4e0d34ef00 Only get the current time once in the DeleteExpired loop 2015-11-30 13:39:27 -05:00
Patrick Mylund Nielsen 901b2413ee Improve cache locality by removing Item-related pointers 2015-11-28 14:47:46 -05:00
Patrick Mylund Nielsen ac0fcef49b Clarify that the OnEvicted function isn't called when an item is overwritten 2015-11-28 12:27:08 -05:00
Patrick Mylund Nielsen 3f2c810ea1 Add OnEvicted() 2015-11-27 22:00:08 -05:00
Patrick Mylund Nielsen a0136a8980 Don't expose the cache mutex 2015-11-27 13:03:24 -05:00
Fabian Ruff fe045e4040 Fix leaking the janitor ticker when shutting down 2015-10-13 12:55:54 +02:00
Patrick Mylund Nielsen 8a2f4f19ef go fmt 2014-12-22 02:48:52 -05:00
Patrick Mylund Nielsen e3a846e1f9 Clarify what NoExpiration and DefaultExpiration are 2014-12-22 02:47:08 -05:00
Patrick Mylund Nielsen a1dd6b7fff s/NewFrom/NewFrom()/ in NewFrom doc 2014-12-22 02:39:59 -05:00
Patrick Mylund Nielsen 9ea75a8586 s/NewFrom/NewFrom()/ in Load/LoadFile/Save/SaveFile docs 2014-12-22 02:37:46 -05:00
Patrick Mylund Nielsen 873e78f0db s/gob.Register/gob.Register()/ in NewFrom doc 2014-12-22 02:30:02 -05:00
Patrick Mylund Nielsen bc36de7dec Clarify NewFrom documentation on use cases 2014-12-22 02:15:12 -05:00
Patrick Mylund Nielsen d68800e607 s/with the same caveats/subject to the same caveat/ in NewFrom doc 2014-12-22 02:09:42 -05:00
Patrick Mylund Nielsen 887753fa90 Refer to DeleteExpired as c.DeleteExpired() 2014-12-22 02:06:10 -05:00
Patrick Mylund Nielsen 888f396121 Update NewFrom doc to be consistent with New 2014-12-22 02:04:11 -05:00
Patrick Mylund Nielsen 46827c6a61 Add NoExpiration and DefaultExpiration constants and use them (thanks to Jason Mooberry) 2014-12-22 01:46:22 -05:00
Patrick Mylund Nielsen 73a30b2033 Add NewFrom, and deprecate Save, SaveFile, Load, and LoadFile 2014-12-22 01:12:10 -05:00
Patrick Mylund Nielsen ffd0b9e3ec Move the unexportedShardedCache to sharded.go 2014-12-22 00:24:49 -05:00
Patrick Mylund Nielsen 6ea7e6fb42 Remove the pointless unexportedInterface 2014-12-22 00:12:48 -05:00
Patrick Mylund Nielsen 7d1d6d6ae9 Note about needing explicit synchronization if you want to use the returned items map and its cache at the same time 2013-08-08 15:10:54 -04:00
Patrick Mylund Nielsen e91e36d111 ItemCount() is equivalent to len(c.Items()), not len(c.Items) 2013-08-08 14:54:03 -04:00
Patrick Mylund Nielsen fd89281dfd s/field/fields/ 2013-08-08 14:53:24 -04:00
Patrick Mylund Nielsen f8cc27acbc Export Item wrapping type, and add an Items() method to cache 2013-08-08 14:53:02 -04:00
Patrick Mylund Nielsen baa5a64da4 Revert 3986bff for now 2013-06-30 22:05:40 -04:00
Patrick Mylund Nielsen 3986bff69b Don't keep the read lock after traversing the items map (before saving to the io.Writer) in Save 2013-06-30 20:20:38 -04:00
Patrick Mylund Nielsen 5388b25b3b Use an RWMutex instead of a Mutex, making Get a read operation only (this slightly changes the eviction behavior: lookup doesn't completely expunge an expired item, but the janitor still will at the next cleanup.) Also, use the same RWMutex in Load and Save (thanks, Alan Shreve) 2013-06-30 20:11:57 -04:00
Patrick Mylund Nielsen c9f5754801 Add O(1) ItemCount method to retrieve items currently in the cache map (whether expired and not cleaned up or not) 2013-04-18 14:32:01 -04:00
Patrick Mylund Nielsen d5d03c28d4 Add specialized increment and decrement methods 2013-04-18 14:24:30 -04:00
Sergey Shepelev a768a84319 Save/LoadFile must close file 2012-09-19 03:25:42 +04:00
Patrick Mylund Nielsen b3a957a46c Handle float64 and int64 Increment/Decrement separately, and don't use Increment(-n) in Decrement to support uints 2012-08-17 13:39:02 +02:00
Patrick Mylund Nielsen 9cc10f6f2f Use a type switch instead, and Use unexported NewSharded in test 2012-08-17 11:35:20 +02:00
Patrick Mylund Nielsen 13225a8ae4 Unexport the shardedcache interface and implementation until both the API and implementation are solid 2012-08-17 11:32:24 +02:00
Patrick Mylund Nielsen 15cda21ff4 Add an interface, embed the mutex, and unexport some unnecessarily exported fields 2012-07-04 19:34:37 +01:00
Patrick Mylund Nielsen bf97a87cc7 Cleaner bucket selection 2012-06-22 09:33:27 +01:00