Skip to content

Commit

Permalink
version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Kallen committed Dec 12, 2008
1 parent 1024a91 commit 6af729a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.markdown
Expand Up @@ -79,6 +79,15 @@ With a limit attribute, indices will only store limit + buffer in the cache. As

`Message.count(:all, :conditions => {:sender_id => ...})` will use the cache rather than the database. This happens for "free" -- no additional declarations are necessary.

### Version Numbers ###

class User
version 7
index ...
end

You can increment the version number as you migrate your schema.

### Transactions ###

Because of the parallel requests writing to the same indices, race conditions are possible. We have created a pessimistic "transactional" memcache client to handle the locking issues.
Expand Down Expand Up @@ -136,7 +145,6 @@ Sometimes your code will request the same cache key twice in one request. You ca
#### Step 1: `config/initializers/cache_money.rb` ####

Place this in `config/initializers/cache_money.rb`

require 'cache_money'

config = YAML.load(IO.read(File.join(RAILS_ROOT, "config", "memcache.yml")))[RAILS_ENV]
Expand Down
2 changes: 1 addition & 1 deletion lib/cash/accessor.rb
Expand Up @@ -66,7 +66,7 @@ def expire(key)
end

def cache_key(key)
"#{name}/#{key.to_s.gsub(' ', '+')}"
"#{name}:#{cache_config.version}/#{key.to_s.gsub(' ', '+')}"
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/cash/config.rb
Expand Up @@ -10,6 +10,7 @@ def self.included(a_module)
module ClassMethods
def self.extended(a_class)
class << a_class
attr_reader :cache_config
delegate :repository, :indices, :to => :@cache_config
alias_method_chain :inherited, :cache_config
end
Expand All @@ -25,6 +26,10 @@ def index(attributes, options = {})
(@cache_config.indices.unshift(Index.new(@cache_config, self, attributes, options))).uniq!
end

def version(number)
@cache_config.options[:version] = number
end

def cache_config=(config)
@cache_config = config
end
Expand All @@ -50,6 +55,10 @@ def ttl
@options[:ttl]
end

def version
@options[:version] || 1
end

def indices
@indices ||= active_record == ActiveRecord::Base ? [] : [Index.new(self, active_record, active_record.primary_key)]
end
Expand Down
18 changes: 14 additions & 4 deletions spec/cash/accessor_spec.rb
Expand Up @@ -22,8 +22,8 @@ module Cash
describe 'when there is a total cache miss' do
it 'yields the keys to the block' do
Story.fetch(["yabba", "dabba"]) { |*missing_ids| ["doo", "doo"] }.should == {
"Story/yabba" => "doo",
"Story/dabba" => "doo"
"Story:1/yabba" => "doo",
"Story:1/dabba" => "doo"
}
end
end
Expand All @@ -32,8 +32,8 @@ module Cash
it 'yields just the missing ids to the block' do
Story.set("yabba", "dabba")
Story.fetch(["yabba", "dabba"]) { |*missing_ids| "doo" }.should == {
"Story/yabba" => "dabba",
"Story/dabba" => "doo"
"Story:1/yabba" => "dabba",
"Story:1/dabba" => "doo"
}
end
end
Expand Down Expand Up @@ -145,5 +145,15 @@ module Cash
end
end
end

describe '#cache_key' do
it 'uses the version number' do
Story.version 1
Story.cache_key("foo").should == "Story:1/foo"

Story.version 2
Story.cache_key("foo").should == "Story:2/foo"
end
end
end
end

0 comments on commit 6af729a

Please sign in to comment.