Permalink
Browse files

Cashier.clear now expires all fragments and tags associated, includes…

… updates to readme and specs
  • Loading branch information...
1 parent e65b052 commit f0d304ea63e3ccedf7811afa5d2c4743712c5e0f @amoslanka amoslanka committed Mar 6, 2013
Showing with 38 additions and 6 deletions.
  1. +9 −3 README.md
  2. +14 −1 lib/cashier.rb
  3. +15 −2 spec/lib/cashier_spec.rb
View
@@ -58,7 +58,7 @@ Rails.cache.write("foo", "bar", :tag => ["some_tag"])
# what's cached
Cashier.tags
-# sweep all stored keys
+# Clears out all tagged keys and tags
Cashier.clear
```
@@ -157,10 +157,16 @@ ActiveSupport::Notifications.subscribe("store_fragment.cashier") do |name, start
end
+# Subscribe to the expire event.
+# payload[:data] will be the list of tags expired.
+ActiveSupport::Notifications.subscribe("expire.cashier") do |name, start, finish, id, payload|
+
+end
+
# Subscribe to the clear event. (no data)
ActiveSupport::Notifications.subscribe("clear.cashier") do |name, start, finish, id, payload|
-
-end
+
+end
# Subscribe to the delete_cache_key event
# this event will fire every time there's a Rails.cache.delete with the key
View
@@ -56,6 +56,7 @@ def expire(*tags)
tags.each do |tag|
fragment_keys = adapter.get_fragments_for_tag(tag)
+ # Delete each fragment from the cache.
fragment_keys.each do |fragment_key|
Rails.cache.delete(fragment_key)
end
@@ -90,6 +91,17 @@ def tags
#
def clear
ActiveSupport::Notifications.instrument("clear.cashier") do
+ # delete them from the cache
+ tags.each do |tag|
+ fragment_keys = adapter.get_fragments_for_tag(tag)
+ # Delete each fragment from the cache.
+ fragment_keys.each do |fragment_key|
+ Rails.cache.delete(fragment_key)
+ end
+ # Delete the tag itself
+ adapter.delete_tag(tag)
+ end
+
adapter.clear
end
end
@@ -118,7 +130,8 @@ def keys_for(tag)
adapter.get_fragments_for_tag(tag)
end
- # Public: adapter which is used by cashier.
+ # Public: adapter which is used by cashier.
+ # Defaults to :cache_store
#
# Examples
#
View
@@ -8,6 +8,7 @@
subject { Cashier }
let(:adapter) { Cashier.adapter }
+ let(:cache) { Rails.cache }
describe "#store_fragment" do
it "should write the tag to the cache" do
@@ -88,7 +89,7 @@
subject.store_fragment('key2', 'settings')
subject.store_fragment('key3', 'email')
- subject.tags.should eql(%w(dashboard settings email))
+ subject.tags.sort.should eql(%w(dashboard settings email).sort)
end
end
@@ -99,11 +100,23 @@
subject.store_fragment('key3', 'email')
end
- it "should expire all tags" do
+ it "should be able to be cleared" do
adapter.should_receive(:clear)
subject.clear
end
+ it "should expire all tagged fragments" do
+ cache.write('a_cache_key', 'foo', tag: 'dashboard')
+ cache.exist?('a_cache_key').should be_true
+ subject.clear
+ cache.exist?('a_cache_key').should be_false
+ end
+
+ it "should expire all tags" do
+ subject.clear
+ cache.exist?('a_cache_key').should be_false
+ end
+
it "should clear the list of tracked tags" do
subject.clear
adapter.tags.should == []

0 comments on commit f0d304e

Please sign in to comment.