Puts a cap on your queries
A caching layer for ActiveRecord (2.3.x and 3.1.x)
Developed and used on zendesk.com.
Sponsored by Zendesk - Enlightened Customer Support
Kasket is a safe way to cache your database queries in memcached. Designed to be as small and simple as possible, and to get out of the way when it is not safe to cache.
You can configure exactly what models to cache and what type of queries to cache.
Collection caching as well as caching of single instances
Automatic cache expiry on database migration
Automatic cache expiry in Kasket udates
Very small code base
Setting up Kasket
Kasket is set up by simply calling Kasket.setup in an initializer script. This will include the required modules into the ActiveRecord
By default, Kasket will cache instance collection with a maximum length of 100. You can everride this by passing the :max_collection_size option to the Kasket.setup call:
Kasket.setup(:max_collection_size => 50)
Configuring caching of your models
You can configure Kasket for any ActiveRecord model, and subclasses will automatically inherit the caching configuration.
If you have an Account model, you can can do the simplest caching configuration like:
This will add a caching index on the id attribute of the Account model, and will make sure that all your calls like Account.find(1) and Account.find_by_id(1) will be cached. All other calls ( say Account.find_by_subdomain('zendesk') ) are untouched.
If you wanted to configure a caching index on the subdomain attribute of the Account model, you would simply write
This would add caching to calls like:
and all other ways of expressing lookups on subdomain.
The goal of Kasket is to be as safe as possible to use, so the cache is expired in a number of situations
When you save a model instance
When your database schema changes
When you install a new version of Kasket
When you ask it to
Cahce expiry on instance save
When you save a model instance, Kasket will calculate the cache entries to expire.
Cache expiry on database schema changes
All Kasket cache keys contain a hash of the column names of the table associated with the model. If you somehow change your table schema, all cache entries for that table will automatically expire
Cache expiry on Kasket upgrades
All Kasket cache keys contain the Kasket version number, so upgrading Kasket will expire all Kasket cache entries.
Manually expiring caches
If you have model methods that update the database behind the back of ActiveRecord, you need to mark these methods as beeing dirty.
This will make sure the clear the cache entries for the current instance when you call update_last_action
How does this work?
We have only used and tested Kasket on MySql.
Let us know if you find any.
Isn't this what Cache Money does?
Absolutely, but cache money does so much more.
Cache Money has way more features than what we need
The Cache Money code is overly complex
Cache Money seems abandoned
Note on Patches/Pull Requests
Fork the project.
Make your feature addition or bug fix.
Add tests for it. This is important so I don't break it in a future version unintentionally.
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
Send me a pull request. Bonus points for topic branches.
Copyright and license
Copyright 2013 Zendesk
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.