andys / redrecord Goto Github PK
View Code? Open in Web Editor NEWCached ActiveRecord fields stored in Redis
Cached ActiveRecord fields stored in Redis
redrecord --------- This gem pre-caches your ActiveRecord model's calculated attributes (computed fields) in Redis. Example ------- class User < ActiveRecord::Base redis_cache do def fullname firstname + ' ' + lastname end end end Methods defined inside the "cache" block are redefined to get the answer from redis first. The cached attributes are saved whenever the record is saved, in an after_commit callback. Cache invalidation for associations ----------------------------------- Redrecord can be used to cache attributes that use assocations: class User < ActiveRecord::Base has_many :preferences redis_cache do def preferences_list preferences.map(&:name).join(', ') end end end class Preference < ActiveRecord::Base belongs_to :user invalidate_cache_on :user end In this example, whenever a preference is saved, the associated user record will be recalculated and saved in redis. If it is an array (eg. has_many) then all of the associated records will be re-cached. Other instance methods of interest: obj.remove_from_cache! # Remove redis cache for an object. obj.add_to_cache! # Recalculate fields and store in redis. obj.cached_fields # hash of the cached fields and their values obj.attribs_with_cached_fields # cached_fields merged with AR attributes obj.verify_cache! # raise an error if cache doesn't match calculated If redis is down? ----------------- To enable talking to a redis server, set Redrecord.enabled=true If Redrecord can't access redis within a timeout (Redrecord.timeout=15 by default) then it sets enabled=false, disabling all redis access so your application can keep working without the cache. For testing purposes, you can set Redrecord.write_only = true This keeps the cache contents up to date but does not read back any cached content. Setup ----- in your Gemfile: gem 'redrecord' in your rails app config/initializers/redrecord.rb: $redis = Redis.new(:host => 'localhost', :port => 6379, :db => 1) Redrecord.redis = $redis Redrecord.enabled = true Contact the author ------------------ Andrew Snow <[email protected]> Andys^ on irc.freenode.net
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.