Moved to github.com/zendesk/kasket¶ ↑
A caching layer for ActiveRecord (2.3.x and 3.1.x)
Developed and used on zendesk.com.
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.
-
Declarative configuration
-
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
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)
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:
Account.has_kasket
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
Account.has_kasket_on :subdomain
This would add caching to calls like:
-
Account.find_by_subdomain(‘zendesk’)
-
Account.find_all_by_subdomain(‘zendesk’)
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
When you save a model instance, Kasket will calculate the cache entries to expire.
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
All Kasket cache keys contain the Kasket version number, so upgrading Kasket will expire all Kasket cache entries.
If you have model methods that update the database behind the back of ActiveRecord, you need to mark these methods as beeing dirty.
Account.kasket_dirty_methods :update_last_action
This will make sure the clear the cache entries for the current instance when you call update_last_action
We have only used and tested Kasket on MySql.
Let us know if you find any.
-
ActiveRecord 2.3.x
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
-
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 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
www.apache.org/licenses/LICENSE-2.0
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.