Permalink
Browse files

mostly documentation cleanup

  • Loading branch information...
1 parent 41402d9 commit 8a715652db7754fcc7a4a78e40176df2aa0f254e @levicook committed Feb 15, 2010
Showing with 65 additions and 38 deletions.
  1. +43 −1 README.rdoc
  2. +1 −16 Rakefile
  3. +1 −1 examples/main.rb
  4. +3 −4 lib/memodis.rb
  5. +17 −16 lib/memodis/dist_cache.rb
View
@@ -1,6 +1,48 @@
= memodis
-Description goes here.
+semi-transparent memoization; backed by redis;
+
+writes are sent to the master, reads are sent to an approriate slave.
+
+Two features:
+1. Memodis#memoize
+1. Memodis::DistCache
+
+== Example
+
+ def fib(num)
+ return num if num < 2
+ fib(num - 1) + fib(num - 2)
+ end
+
+ puts "Before memoize: "
+ puts fib(33) => 3524578 # after ~ 7 seconds
+
+ extend Memodis
+ memoize :fib, Memodis::DistCache.new({
+ :key_gen => lambda { |k| "fib(#{k})" },
+ :decoder => :integer,
+ :expires => (10 * 60),
+ :master => '127.0.0.1:16379 127.0.0.1:16380'.split,
+ :slaves => '127.0.0.1:16389 127.0.0.1:16390
+ 127.0.0.1:16391 127.0.0.1:16392
+ 127.0.0.1:16393 127.0.0.1:16394'.split
+ })
+
+ puts "After memoize: "
+ puts fib(33) => 3524578 # after ~ 0.03 seconds
+ puts fib(33) => 3524578 # after ~ 0.0001 seconds
+ puts fib(33) => 3524578 # after ~ 0.0001 seconds
+
+
+== Background
+1. http://blog.grayproductions.net/articles/caching_and_memoization
+1. http://code.google.com/p/redis & http://github.com/ezmobius/redis-rb
+
+== Important Moving Parts
+1. http://code.google.com/p/redis/wiki/GetCommand
+1. http://code.google.com/p/redis/wiki/SetCommand
+1. http://code.google.com/p/redis/wiki/SetnxCommand
== Note on Patches/Pull Requests
View
@@ -6,21 +6,7 @@ begin
Jeweler::Tasks.new do |gem|
gem.name = "memodis"
gem.summary = %Q{redis backed memoization helpers}
- gem.description = %Q{
-semi-transparent memoization; backed by redis; redis-rb and redis-namespace
-
-Background
------------
-1) http://blog.grayproductions.net/articles/caching_and_memoization
-2) http://code.google.com/p/redis & http://github.com/ezmobius/redis-rb
-
-Important Moving Parts
-----------------------
-1) http://code.google.com/p/redis/wiki/GetCommand
-2) http://code.google.com/p/redis/wiki/SetCommand
-3) http://code.google.com/p/redis/wiki/SetnxCommand
-4) http://github.com/defunkt/redis-namespace
-}
+ gem.description = %Q{ semi-transparent memoization; backed by redis; }
gem.email = "levicook@gmail.com"
gem.homepage = "http://github.com/levicook/memodis"
gem.authors = ["levicook@gmail.com"]
@@ -86,7 +72,6 @@ task :default => :test
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "memodis #{version}"
rdoc.rdoc_files.include('README*')
View
@@ -25,7 +25,7 @@ def fib(num)
127.0.0.1:16393 127.0.0.1:16394'.split
})
-puts "\nAfter memoize: "
+puts "After memoize: "
puts Hitimes::Interval.measure { print fib(33); print ': ' }
puts Hitimes::Interval.measure { print fib(33); print ': ' }
puts Hitimes::Interval.measure { print fib(33); print ': ' }
View
@@ -8,10 +8,9 @@ module Memodis
# slurp cool vendor goodies into our namespace. would declare them
# as gem dependencies, but they don't seem to be published...
- VENDOR_PATH = Pathname.new(__FILE__).parent.parent + 'vendor'
-
- class_eval((VENDOR_PATH+'memoizable.rb').read) unless defined? Memodis::Memoizable
- class_eval((VENDOR_PATH+'weak_cache.rb').read) unless defined? Memodis::WeakCache
+ vendor_path = Pathname.new(__FILE__).parent.parent + 'vendor'
+ class_eval((vendor_path+'memoizable.rb').read) unless defined? Memodis::Memoizable
+ class_eval((vendor_path+'weak_cache.rb').read) unless defined? Memodis::WeakCache
include Memodis::Memoizable
View
@@ -9,13 +9,11 @@ class DistCache
CODERS.freeze
def initialize(options)
-
@master = DistRedis.new({
:db => options[:db],
:hosts => options[:master],
:timeout => options[:timeout],
})
-
@slaves = options[:slaves].map do |h|
host, port = h.split(':')
Redis.new({
@@ -25,30 +23,20 @@ def initialize(options)
:timeout => options[:timeout],
})
end
-
- @encoder = case options[:encoder]
- when Proc; options[:encoder]
- else; CODERS[options[:encoder]]
- end
-
- @decoder = case options[:decoder]
- when Proc; options[:decoder]
- else; CODERS[options[:decoder]]
- end
-
+ @encoder = resolve_coder(options[:encoder])
+ @decoder = resolve_coder(options[:decoder])
@key_gen = options.fetch(:key_gen, lambda { |k| k })
-
@expires = options[:expires]
end
def []= key, val
- key = @key_gen.call(key)
+ key = canonicalize(key)
@master.set(key, encode(val))
@master.expire(key, @expires) unless @expires.nil?
end
def [] key
- key = @key_gen.call(key)
+ key = canonicalize(key)
if val = get(key)
decode(val)
else
@@ -58,6 +46,10 @@ def [] key
private
+ def canonicalize(key)
+ @key_gen.call(key)
+ end
+
def indexed_slaves
@indexed_slaves ||= @slaves.inject(Hash.new) do |index, slave|
slave_info = slave.info
@@ -83,6 +75,15 @@ def encode(val)
@encoder.call(val)
end
+ def resolve_coder(coder_spec)
+ case coder_spec
+ when Proc
+ coder_spec
+ else
+ CODERS[coder_spec]
+ end
+ end
+
end
end

0 comments on commit 8a71565

Please sign in to comment.