cachecataz 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/lib/cachecataz.rb CHANGED
@@ -6,11 +6,13 @@ module Cachecataz
6
6
  # config.after_initialize do
7
7
  # Cachecataz.enable = true
8
8
  # Cachecataz.provider = Rails.cache
9
+ # Cachecataz.random = false
9
10
  # end
10
- Config = {:api => {:get => :read, :set => :write, :incr => :increment, :exist? => :exist?},
11
+ Config = {:api => {:get => :read, :set => :write, :exist? => :exist?},
11
12
  :enabled => false,
12
13
  :ns_delim => ":",
13
- :index_delim => "/" }
14
+ :index_delim => "/",
15
+ :random => true }
14
16
 
15
17
  # Config method to enable Cachecataz
16
18
  #
@@ -19,6 +21,13 @@ module Cachecataz
19
21
  Config[:enabled] = val
20
22
  end
21
23
 
24
+ # Config method to randomize the seed for namespaces in Cachecataz. (default to true, the recommended setting)
25
+ #
26
+ # @param [Boolean] val
27
+ def self.random=(val)
28
+ Config[:random] = val
29
+ end
30
+
22
31
  # Set custom delimiter if desired
23
32
  def self.delim=(val)
24
33
  Config[:ns_delim] = val.first rescue ":"
@@ -49,17 +58,20 @@ module Cachecataz
49
58
 
50
59
  api_method = api_args.slice!(0)
51
60
 
52
- if Config[:api][api_method].respond_to?(:call)
61
+ case
62
+ when Config[:api][api_method].respond_to?(:call)
53
63
  Config[:api][api_method].call(*api_args)
64
+ when Config[:provider].respond_to?(Config[:api][api_method])
65
+ Config[:provider].send(Config[:api][api_method], *api_args)
54
66
  else
55
- Config[:provider].send(Config[:api][api_method], *api_args)
67
+ raise "Unknown method for provider: #{Config[:provider]}"
56
68
  end
57
69
  end
58
70
 
59
71
  # Method that validates the api and provider if they are defined in configuration
60
72
  def self.validate_api(api={})
61
- unless api.include?(:get) && api.include?(:set) && api.include?(:incr) && api.include?(:exist?)
62
- raise "Unknown api methods, define [:get, :set, :incr, :exist?] to use cachecataz with a non-standard provider"
73
+ unless api.include?(:get) && api.include?(:set) && api.include?(:exist?)
74
+ raise "Unknown api methods, define [:get, :set, :exist?] to use cachecataz with a non-standard provider"
63
75
  end
64
76
  end
65
77
 
@@ -87,7 +99,7 @@ module Cachecataz
87
99
  def cache_point(point_key, scope_hash)
88
100
  c_scope = @_point_keys[point_key]
89
101
  c_point = c_scope.inject(point_key.to_s){|s, i| s << Cachecataz::Config[:ns_delim] << (scope_hash[i.to_s] || scope_hash[i]).to_s }
90
- Cachecataz[:set, c_point, "0"] if !Cachecataz[:exist?, c_point]
102
+ Cachecataz[:set, c_point, Cachecataz::Config[:random] ? rand(10000).to_s : "0"] if !Cachecataz[:exist?, c_point]
91
103
  return c_point
92
104
  end
93
105
 
@@ -110,7 +122,7 @@ module Cachecataz
110
122
  # @param [Hash] scope_hash the data provider for the scope of the namespace
111
123
  def expire_namespace(point_key, scope_hash={})
112
124
  c_point = cache_point(point_key, scope_hash)
113
- Cachecataz[:incr, c_point]
125
+ Cachecataz[:set, c_point, (Cachecataz[:get, c_point].to_i + 1 rescue rand(10000)).to_s]
114
126
  end
115
127
 
116
128
  # Class level method to expire all the namespace of cachecataz for a given data provider
@@ -5,6 +5,7 @@ describe MockModel do
5
5
 
6
6
  before(:all) do
7
7
  Cachecataz.enable = false
8
+ Cachecataz.random = false
8
9
  end
9
10
 
10
11
  it "is disabled on initialization for cache_scope :user" do
@@ -22,6 +23,7 @@ describe MockModel do
22
23
  before(:all) do
23
24
  Cachecataz.enable = true
24
25
  Cachecataz.provider = MockCache.new
26
+ Cachecataz.random = false
25
27
  end
26
28
 
27
29
  it "returns a namespace key for an empty cache_scope" do
@@ -96,6 +98,7 @@ describe MockModel do
96
98
  before(:all) do
97
99
  Cachecataz::Config[:provider].clear # not part of api, just an easy way to clear the mock cache
98
100
  Cachecataz.delim = ["|", "/"]
101
+ Cachecataz.random = false
99
102
  end
100
103
 
101
104
  it "returns a namespace key with index for cache_scope :user, :id" do
@@ -111,6 +114,7 @@ describe MockModel do
111
114
  before(:all) do
112
115
  Cachecataz::Config[:provider].clear # not part of api, just an easy way to clear the mock cache
113
116
  Cachecataz.delim = [":", "|"]
117
+ Cachecataz.random = false
114
118
  end
115
119
 
116
120
  it "returns a namespace key with index for cache_scope :user, :id" do
@@ -130,6 +134,7 @@ describe MockModel do
130
134
  :set => lambda{ |*args| Cachecataz::Config[:provider].all(:write, *args) },
131
135
  :incr => lambda{ |*args| Cachecataz::Config[:provider].all(:increment, *args) },
132
136
  :exist? => lambda{ |*args| Cachecataz::Config[:provider].all(:exist?, *args) }}
137
+ Cachecataz.random = false
133
138
  end
134
139
 
135
140
  it "returns a namespace key for an empty cache_scope" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cachecataz
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brandon Dewitt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-14 00:00:00 -04:00
18
+ date: 2011-03-20 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency