-
Notifications
You must be signed in to change notification settings - Fork 5
Add cleanup block to FastCache::Cache, with RSpec tests. #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,13 +32,18 @@ class Cache | |
| # the cache. | ||
| # @param [Integer] expire_interval Number of cache operations between | ||
| # calls to {#expire!}. | ||
| def initialize(max_size, ttl, expire_interval = 100) | ||
| # @yield [Object] If a block is given, each time an object is removed | ||
| # from the cache, it will be yielded to the block. This | ||
| # is useful for cleaning up resources used by objects | ||
| # stored in the cache. | ||
| def initialize(max_size, ttl, expire_interval = 100, &cleanup) | ||
| @max_size = max_size | ||
| @ttl = ttl.to_f | ||
| @expire_interval = expire_interval | ||
| @op_count = 0 | ||
| @data = {} | ||
| @expires_at = {} | ||
| @cleanup = cleanup | ||
| end | ||
|
|
||
| # Retrieves a value from the cache, if available and not expired, or | ||
|
|
@@ -84,6 +89,7 @@ def delete(key) | |
| entry = @data.delete(key) | ||
| if entry | ||
| @expires_at.delete(entry) | ||
| @cleanup.call(entry.value) if @cleanup | ||
| entry.value | ||
| else | ||
| nil | ||
|
|
@@ -103,6 +109,7 @@ def empty? | |
| # | ||
| # @return [self] | ||
| def clear | ||
| @data.each_value {|entry| @cleanup.call(entry.value)} if @cleanup | ||
| @data.clear | ||
| @expires_at.clear | ||
| self | ||
|
|
@@ -176,6 +183,7 @@ def get(key) | |
| if found | ||
| if entry.expires_at <= t | ||
| @expires_at.delete(entry) | ||
| @cleanup.call(entry.value) if @cleanup | ||
| return false, nil | ||
| else | ||
| @data[key] = entry | ||
|
|
@@ -194,7 +202,9 @@ def store(key, val) | |
| end | ||
|
|
||
| def store_entry(key, entry) | ||
| @data.delete(key) | ||
| found = true | ||
| old_entry = @data.delete(key) { found = false } | ||
| @cleanup.call(old_entry.value) if @cleanup && found | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not check for a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been a while since I wrote this, but if I recall correctly, the reason for this was to properly handle the case where a |
||
| @data[key] = entry | ||
| @expires_at[entry] = key | ||
| shrink_if_needed | ||
|
|
@@ -204,6 +214,7 @@ def shrink_if_needed | |
| if @data.length > @max_size | ||
| entry = delete(@data.shift) | ||
| @expires_at.delete(entry) | ||
| @cleanup.call(entry.value) if @cleanup | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -212,7 +223,8 @@ def check_expired(t) | |
| while (key_value_pair = @expires_at.first) && | ||
| (entry = key_value_pair.first).expires_at <= t | ||
| key = @expires_at.delete(entry) | ||
| @data.delete(key) | ||
| entry = @data.delete(key) | ||
| @cleanup.call(entry.value) if @cleanup | ||
| end | ||
| end | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the semantics of returning a value that has been cleaned up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. As implemented, the value will still be returned here after the cleanup block has been called. I think that's ok, as long as the user is aware that's the case -- if they are invalidating the object in some way with their cleanup block, they should not treat the returned value here as something usable. (But, might still be useful for identity comparison or something like that.)
An alternative, I suppose, could be to return
nilhere if there is a cleanup block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, we could change it to not call the cleanup block here? That would seem inconsistent to me, though.