caching 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/caching/version.rb +1 -1
  2. data/lib/caching.rb +7 -4
  3. metadata +2 -2
@@ -1,3 +1,3 @@
1
1
  module Caching
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/caching.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  Dir["#{File.dirname(__FILE__)}/caching/*.rb"].each { |file| require file }
2
+ require 'set'
3
+ require 'base64'
2
4
 
3
5
  module Caching
4
6
 
@@ -8,7 +10,8 @@ module Caching
8
10
  alias_method "#{method}_without_cache", method
9
11
 
10
12
  define_method method do |*args, &block|
11
- method_key = "#{method}_#{Marshal.dump(args)}"
13
+ args_key = args.any? ? "_#{Base64.encode64(Marshal.dump(args))}" : ''
14
+ method_key = "#{method}#{args_key}"
12
15
  cached_methods_keys[method] << method_key
13
16
  cache_storage.fetch(method_key) { send "#{method}_without_cache", *args, &block }
14
17
  end
@@ -23,15 +26,15 @@ module Caching
23
26
  end
24
27
 
25
28
  def cached_methods_keys
26
- @cached_methods_keys ||= Hash.new {|h,k| h[k] = []}
29
+ @cached_methods_keys ||= Hash.new {|h,k| h[k] = Set.new}
27
30
  end
28
31
 
29
32
  def clear_cache(*methods)
30
33
  method_keys = cached_methods_keys.
31
34
  select{ |m,_| methods.include? m }.
32
- flat_map{ |_,keys| keys }
33
-
35
+ flat_map{ |_,keys| keys.to_a }
34
36
  cache_storage.clear *method_keys
37
+ method_keys.each { |k| cached_methods_keys.delete k }
35
38
  end
36
39
 
37
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caching
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-31 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler