README.md in cashier-0.4.0 vs README.md in cashier-0.4.1
- old
+ new
@@ -27,14 +27,26 @@
caches_action :tag => proc {|c|
# c is the controller
"users/#{c.current_user.id}/dashboard"
}
-# in your sweeper, in your observers, in your resque jobs...wherever
+# in your sweeper, in your observers, in your Resque jobs...wherever
Cashier.expire 'complicated-action'
Cashier.expire 'tag1', 'tag2', 'tag3', 'tag4'
+# It integrates smoothly with Rails.cache as well, not just the views
+Rails.cache.fetch("user_1", :tag => ["users"]) { User.find(1) }
+Rails.cache.fetch("user_2", :tag => ["users"]) { User.find(2) }
+Rails.cache.fetch("user_3", :tag => ["users"]) { User.find(3) }
+Rails.cache.fetch("admins", :tag => ["users"]) { User.where(role: "Admin").all }
+
+# You can then expire all your users
+Cashier.expire "users"
+
+# You can also use Rails.cache.write
+Rails.cache.write("foo", "bar", :tag => ["some_tag"])
+
# what's cached
Cashier.tags
# sweep all stored keys
Cashier.clear
@@ -75,23 +87,23 @@
**IMPORTANT**: this store is ONLY for the tags, your fragments will still be stored in `Rails.cache`.
#### Setting an adapter for working with the cache as the tags storage
-`config/initializers/cashier.rb`
-
```ruby
-Cachier.adapter = :cache_store
+# config/environment/production.rb
+
+config.cashier.adapter = :cache_store
+# or config.cashier.adapter = :redis_store
```
#### Setting an adapter for working with Redis as the tags storage
-`config/initializers/cashier.rb`
```ruby
-Cashier.adapter = :redis_store
-Cashier.adapter.redis = Redis.new(:host => '127.0.0.1', :port => '3697')
+# config/environment/production.rb
+config.cashier.adapter.redis = Redis.new(:host => '127.0.0.1', :port => '3697') # or Resque.redis or any existing redis connection
```
### Why Redis?
The reason Redis was introduced is that while the Rails.cache usage
@@ -116,57 +128,57 @@
tag2 = (0...50).map{ ('a'..'z').to_a[rand(26)] }.join
Cashier.store_fragment(key, tag, tag2)
end
end
```
-
Using the Redis adapter, the same piece of code takes 0.8 seconds, quite the difference :)
-## Testing
-Use can use cashier to test caching as well. First things first:
+### Notifications
-```ruby
-# test.rb
+Cashier will send out events when things happen inside the library.
+The events are sent out through `ActiveSupport::Notifications` so you can pretty much subscribe to the events from anywhere you want.
-config.application_controller.perform_caching = true
-```
+Here are the way you can subscribe to the events and use the data from them.
-I've also included some Rspec Matchers and a cucumber helper for testing
-caching. The rspec matchers can be used like this:
-
```ruby
-describe "get index" do
- include Cashier::Matchers
+ # Subscribe to the store fragment event, this is fired every time cashier will call the "store_fragment" method
+ # payload[:data] will be something like this: ["key", ["tag1", "tag2", "tag3"]]
+ ActiveSupport::Notifications.subscribe("store_fragment.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
+
+ # Subscribe to the delete_cache_key event
+ # this event will fire every time there's a Rails.cache.delete with the key
+ # payload[:data] will be the key name that's been deleted from the cache
+ ActiveSupport::Notifications.subscribe("delete_cache_key.cashier") do |name, start, finish, id, payload|
+
+ end
- it "should cache the action" do
- get :index
- 'some-tag'.should be_cached
- end
-end
+ # Subscribe to the o_write_cache_key event
+ # this event will fire every time there's a Rails.cache.write with the key
+ # payload[:data] will be the key name that's been written to the cache
+ ActiveSupport::Notifications.subscribe("write_cache_key.cashier") do |name, start, finish, id, payload|
+
+ end
```
-Testing w/cucumber is more involved.
+### Notifications use case
+At [Gogobot](http://www.gogobot.com) we have a plugin to invalidate the external CDN cache on full pages for logged out users.
+The usage is pretty unlimited.
-```ruby
-# features/support/cashier.rb
-require 'cashier/cucumber'
-```
+If you think we're missing a notification, please do open an issue or be awesome and do it yourself and open a pull request.
-is an example of a possible step
-
-```ruby
-Then /the dashboard should be cached/ do
- "dashboard".should be_cached
-end
-```
-Including `cashier/cucumber` will also wipe the cache before every
-scenario.
-
## Contributors
-* [adman65](http://twitter.com/adman65) - Initial Implementation
+* [twinturbo](http://twitter.com/adman65) - Initial Implementation
* [KensoDev](http://twitter.com/kensodev) - Adding Redis support (Again \o/)
+* [KensoDev](http://twitter.com/kensodev) - Adding plugins support for callback methods
## Contributing to Cashier
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it