-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add cleanup block to FastCache::Cache, with RSpec tests. #3
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -39,6 +39,7 @@ def initialize(max_size, ttl, expire_interval = 100) | |
@op_count = 0 | ||
@data = {} | ||
@expires_at = {} | ||
@cleanup = Proc.new if block_given? | ||
end | ||
|
||
# Retrieves a value from the cache, if available and not expired, or | ||
|
@@ -84,6 +85,7 @@ def delete(key) | |
entry = @data.delete(key) | ||
if entry | ||
@expires_at.delete(entry) | ||
@cleanup.call(entry.value) if @cleanup | ||
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. 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 commentThe 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 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. Or, we could change it to not call the cleanup block here? That would seem inconsistent to me, though. |
||
entry.value | ||
else | ||
nil | ||
|
@@ -103,6 +105,7 @@ def empty? | |
# | ||
# @return [self] | ||
def clear | ||
@data.values.map(&:value).map(&@cleanup) if @cleanup | ||
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. This operation is unnecessarily inefficient:
What you want here is a single |
||
@data.clear | ||
@expires_at.clear | ||
self | ||
|
@@ -176,6 +179,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 +198,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 | ||
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 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 +210,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 +219,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.
Please provide the block as an
¶m
. This type of defaulting is one of the obscure features of Ruby that saves a few characters at the expense of readability.