CachedSupermodel
by Adocca AB
http://rubyforge.org/projects/adocca-plugins
== DESCRIPTION:
A library that overloads lots of methods in ActiveRecord::Base and ActiveRecord::Associations
to automatically cache all descendants of ActiveRecord::Base and their associations and finders
etc in memcached using the AdoccaMemcache gem.
Based on cached_model by <a href="http://dev.robotcoop.com/">robotcoop</a>. We started out with
their code, but added so much that it merited its own plugin.
Apologies for not doing proper contributions to their project instead of building our own, but
we are limited by commercial requirements rather than open source idealism.
=== Cached Super Model: a short introduction.
This works a lot like <tt>acts_as_cached_model</tt>, but with a few subtle differences.
CachedSuperModel caches more aggressively than <tt>cached_model</tt>.
When using conditions or joins, it will not cache, so you need to develop with the caching in mind.
The good old <tt>cached_model</tt> only caches on find_by_id => 1 but CachedSuperModel caches all associations.
So you get to declare <tt>cached_finders</tt>. Example:
cached_finder :find_all_by_user_id_and_password_and_hat
Then, when run, that query will be cached.
== FEATURES/PROBLEMS:
* Through associations are not invalidated _at_all_ in the end association right now. Ex:
* User has_many :taggings
* User has_many :tags, :through => :taggings, :source => :tag
* If you now remove tags without removing taggings the user will still have those tags in the User#tags collection.
== SYNOPSYS:
To completely remove most of your SQL read queries, this is what you write (basically) in your environment.rb.
I use the caffeine gem here to get as speedy access to the memcache servers as possible, just 'gem install caffeine' for that.
Also, you need to install the memcache_client gem ('gem install memcache_client') because otherwise rails will assume (stupid
as it is) that you can't use memcache for sessions.
-------------------8<--------------------
# require the necessary gems
require 'caffeine'
require 'adocca_memcache'
require 'cached_supermodel'
# Set the base time to live to "forever"
ActiveRecord::Base.ttl = 0
# default memcache options
# including a separate namespace for each environment (dev, prod, test).
# (if you want to run several apps on the same memcache servers i suggest also adding the app name to the namespace...)
memcache_options = :c_threshold => 10_000,
:compression => false,
:debug => false,
:namespace => "#{RAILS_ENV",
:readonly => false,
:urlencode => false
}
# The default CACHE object used by pretty much everything
CACHE = Caffeine::MemCache.new memcache_options
# Let our magical synchronization lock know about this cache
Adocca::Synchronized::CACHE = CACHE
# set the cache servers to localhost (please, dont forget to actually run a memcache server wherever you set this to :)
CACHE.servers = ['localhost:11211']
#
# Let the sessions be stored in a separate memcached instance
# This way we can easily flush the cache for the application-servers
# without destroying the sessions.
#
CACHE_SESSION = Caffeine::MemCache.new memcache_options
CACHE_SESSION.servers = ['localhost:11211']
# User is considered offline after N mins
$SESSION_TIMEOUT = 12.hours
# create a nicely reachable constant for the session options
DEFAULT_SESSION_OPTIONS = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
DEFAULT_SESSION_OPTIONS.delete(:session_expires)
# use the session memcache for sessions unless we are doing this in the test environment,
# in which case i experienced some problems with this...
DEFAULT_SESSION_OPTIONS.update({
:database_manager => CGI::Session::MemCacheStore,
:cache => CACHE_SESSION,
:expires => $SESSION_TIMEOUT
}) unless RAILS_ENV == 'test'
--------------------8<-------------------------
== REQUIREMENTS:
* AdoccaMemcache
== INSTALL:
sudo gem install CachedSupermodel
sudo gem install memcache_client
(recommended)
sudo gem install caffeine
== LICENSE:
(The MIT License)
Copyright (c) 2006 Albert Ramstedt, Adocca AB
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
by Adocca AB
http://rubyforge.org/projects/adocca-plugins
== DESCRIPTION:
A library that overloads lots of methods in ActiveRecord::Base and ActiveRecord::Associations
to automatically cache all descendants of ActiveRecord::Base and their associations and finders
etc in memcached using the AdoccaMemcache gem.
Based on cached_model by <a href="http://dev.robotcoop.com/">robotcoop</a>. We started out with
their code, but added so much that it merited its own plugin.
Apologies for not doing proper contributions to their project instead of building our own, but
we are limited by commercial requirements rather than open source idealism.
=== Cached Super Model: a short introduction.
This works a lot like <tt>acts_as_cached_model</tt>, but with a few subtle differences.
CachedSuperModel caches more aggressively than <tt>cached_model</tt>.
When using conditions or joins, it will not cache, so you need to develop with the caching in mind.
The good old <tt>cached_model</tt> only caches on find_by_id => 1 but CachedSuperModel caches all associations.
So you get to declare <tt>cached_finders</tt>. Example:
cached_finder :find_all_by_user_id_and_password_and_hat
Then, when run, that query will be cached.
== FEATURES/PROBLEMS:
* Through associations are not invalidated _at_all_ in the end association right now. Ex:
* User has_many :taggings
* User has_many :tags, :through => :taggings, :source => :tag
* If you now remove tags without removing taggings the user will still have those tags in the User#tags collection.
== SYNOPSYS:
To completely remove most of your SQL read queries, this is what you write (basically) in your environment.rb.
I use the caffeine gem here to get as speedy access to the memcache servers as possible, just 'gem install caffeine' for that.
Also, you need to install the memcache_client gem ('gem install memcache_client') because otherwise rails will assume (stupid
as it is) that you can't use memcache for sessions.
-------------------8<--------------------
# require the necessary gems
require 'caffeine'
require 'adocca_memcache'
require 'cached_supermodel'
# Set the base time to live to "forever"
ActiveRecord::Base.ttl = 0
# default memcache options
# including a separate namespace for each environment (dev, prod, test).
# (if you want to run several apps on the same memcache servers i suggest also adding the app name to the namespace...)
memcache_options = :c_threshold => 10_000,
:compression => false,
:debug => false,
:namespace => "#{RAILS_ENV",
:readonly => false,
:urlencode => false
}
# The default CACHE object used by pretty much everything
CACHE = Caffeine::MemCache.new memcache_options
# Let our magical synchronization lock know about this cache
Adocca::Synchronized::CACHE = CACHE
# set the cache servers to localhost (please, dont forget to actually run a memcache server wherever you set this to :)
CACHE.servers = ['localhost:11211']
#
# Let the sessions be stored in a separate memcached instance
# This way we can easily flush the cache for the application-servers
# without destroying the sessions.
#
CACHE_SESSION = Caffeine::MemCache.new memcache_options
CACHE_SESSION.servers = ['localhost:11211']
# User is considered offline after N mins
$SESSION_TIMEOUT = 12.hours
# create a nicely reachable constant for the session options
DEFAULT_SESSION_OPTIONS = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
DEFAULT_SESSION_OPTIONS.delete(:session_expires)
# use the session memcache for sessions unless we are doing this in the test environment,
# in which case i experienced some problems with this...
DEFAULT_SESSION_OPTIONS.update({
:database_manager => CGI::Session::MemCacheStore,
:cache => CACHE_SESSION,
:expires => $SESSION_TIMEOUT
}) unless RAILS_ENV == 'test'
--------------------8<-------------------------
== REQUIREMENTS:
* AdoccaMemcache
== INSTALL:
sudo gem install CachedSupermodel
sudo gem install memcache_client
(recommended)
sudo gem install caffeine
== LICENSE:
(The MIT License)
Copyright (c) 2006 Albert Ramstedt, Adocca AB
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.