cached_resource 1.0.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,34 +1,58 @@
1
- # CachedResource
2
- CachedResource helps ActiveResource by caching responses according to request parameters. It can help reduce the lag created by making repeated requests across the network.
1
+ # CachedResource [![Build Status](https://secure.travis-ci.org/Ahsizara/cached_resource.png)](http://travis-ci.org/Ahsizara/cached_resource)
2
+ CachedResource is a Ruby gem whose goal is to increase the performance of interacting with web services via ActiveResource by caching responses based on request parameters. It can help reduce the lag created by making repeated requests across a network.
3
3
 
4
4
  ## Installation
5
5
  gem install cached_resource
6
6
 
7
7
  ## Configuration
8
- CachedResource works "out of the box" with ActiveResource. By default, it caches responses to an `ActiveSupport::Cache::MemoryStore` and logs to an `ActiveSupport::BufferedLogger` attached to a `StringIO` object. **In a Rails 3 environment**, CachedResource will attach itself to the Rails logger and cache.
8
+ Enable CachedResource across all ActiveResources. You could put this code in config/initializers.
9
9
 
10
- Turn CachedResource off. This will cause all ActiveResource responses to be retrieved normally (i.e. via the network).
10
+ class ActiveResource::Base
11
+ cached_resource
12
+ end
13
+
14
+ Enable CachedResource for a single class.
15
+
16
+ class MyActiveResource < ActiveResource::Base
17
+ cached_resource
18
+ end
19
+
20
+ ### Options
21
+ CachedResource accepts the following options:
22
+
23
+ * `:cache` The cache store that CacheResource should use. Default: The `Rails.cache` if available, or an `ActiveSupport::Cache::MemoryStore`
24
+ * `:ttl` The time in seconds until the cache should expire. Default: `604800`
25
+ * `:logger` The logger to which CachedResource messages should be written. Default: The `Rails.logger` if available, or an `ActiveSupport::BufferedLogger`
26
+ * `:enabled` Default: `true`
27
+
28
+ You can set them like this:
29
+
30
+ cached_resource :cache => MyCacheStore.new, :ttl => 60, :logger => MyLogger.new, :enabled => false
31
+
32
+ You can also change these options on the fly.
33
+
34
+ Turn CachedResource off. This will cause all responses to be retrieved normally (i.e. via the network).
35
+
36
+ MyActiveResource.cached_resource.off!
11
37
 
12
- CachedResource.off!
13
-
14
38
  Turn CachedResource on.
15
39
 
16
- CachedResource.on!
17
-
40
+ MyActiveResource.cached_resource.on!
41
+
18
42
  Set the cache expiry time to 60 seconds.
19
43
 
20
- CachedResource.config.cache_time_to_live = 60
21
-
44
+ MyActiveResource.cached_resource.ttl = 60
45
+
22
46
  Set a different logger.
23
47
 
24
- CachedResource.config.logger = MyLogger.new
25
-
48
+ MyActiveResource.cached_resource.logger = MyLogger.new
49
+
26
50
  Set a different cache store.
27
51
 
28
- CachedResource.config.cache = MyCacheStore.new
52
+ MyActiveResource.cached_resource.cache = MyCacheStore.new
29
53
 
30
54
  ## Usage
31
- Sit back and relax! If you need to reload a particular request you can do something like:
55
+ Sit back and relax! If you need to reload a particular request you can do something like this:
32
56
 
33
57
  MyActiveResource.find(:all, :reload => true)
34
58
 
@@ -36,5 +60,8 @@ Sit back and relax! If you need to reload a particular request you can do someth
36
60
  rake
37
61
 
38
62
  ## Credit/Inspiration
39
- quamen and [this gist](http://gist.github.com/947734)
40
- latimes and [this plugin](http://github.com/latimes/cached_resource)
63
+ * quamen and [this gist](http://gist.github.com/947734)
64
+ * latimes and [this plugin](http://github.com/latimes/cached_resource)
65
+
66
+ ## Future Work
67
+ * Cached collection lookups
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
+ s.add_dependency "rake"
19
20
  s.add_dependency "activeresource"
20
21
  s.add_dependency "activesupport"
21
22
 
@@ -1,41 +1,18 @@
1
- require 'singleton'
2
1
  require 'stringio'
2
+ require 'ostruct'
3
3
 
4
4
  require 'active_support/concern'
5
- require 'cached_resource/config'
5
+ require 'cached_resource/cached_resource'
6
+ require 'cached_resource/nilio'
7
+ require 'cached_resource/configuration'
6
8
  require 'cached_resource/caching'
7
9
  require 'cached_resource/version'
8
10
 
9
11
  module CachedResource
10
-
11
- # Switch cache usage off
12
- def self.off!
13
- self.config.cache_enabled = false
14
- end
15
-
16
- # Switch cache usage on
17
- def self.on!
18
- self.config.cache_enabled = true
19
- end
20
-
21
- # retrieve the configured logger
22
- def self.logger
23
- config.logger
24
- end
25
-
26
- # retrieve the configured cache store
27
- def self.cache
28
- config.cache
29
- end
30
-
31
- # Retrieve the configuration object
32
- def self.config
33
- @@config ||= CachedResource::Config.instance
34
- end
35
-
12
+ # nada
36
13
  end
37
14
 
38
- # Include caching in ActiveResource::Base
15
+ # Include model methods in ActiveResource::Base
39
16
  class ActiveResource::Base
40
- include CachedResource::Caching
17
+ include CachedResource::Model
41
18
  end
@@ -0,0 +1,28 @@
1
+ module CachedResource
2
+ # The Model module is included in ActiveResource::Base and
3
+ # provides methods to enable caching and manipulate the caching
4
+ # configuration
5
+ module Model
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class << self
10
+ attr_accessor :cached_resource
11
+
12
+ # initialize cached resource or retrieve the current cached resource configuration
13
+ def cached_resource(options={})
14
+ defined?(@cached_resource) && @cached_resource || setup_cached_resource!(options)
15
+ end
16
+
17
+ # setup cached resource for this class by creating a new configuration
18
+ # and establishing the necessary methods.
19
+ def setup_cached_resource!(options)
20
+ @cached_resource = CachedResource::Configuration.new(options)
21
+ send :include, CachedResource::Caching
22
+ @cached_resource
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -4,7 +4,6 @@ module CachedResource
4
4
  module Caching
5
5
  extend ActiveSupport::Concern
6
6
 
7
- # when included, setup a middle man for find
8
7
  included do
9
8
  class << self
10
9
  alias_method_chain :find, :cache
@@ -12,12 +11,11 @@ module CachedResource
12
11
  end
13
12
 
14
13
  module ClassMethods
15
-
16
14
  # find a resource using the cache or resend the request
17
15
  # if :reload is set to true or caching is disabled
18
16
  def find_with_cache(*arguments)
19
17
  arguments << {} unless arguments.last.is_a?(Hash)
20
- should_reload = arguments.last.delete(:reload) || !CachedResource.config.cache_enabled
18
+ should_reload = arguments.last.delete(:reload) || !cached_resource.enabled
21
19
  arguments.pop if arguments.last.empty?
22
20
  key = cache_key(arguments)
23
21
 
@@ -33,8 +31,8 @@ module CachedResource
33
31
  # try to find a cached response for the given key. If
34
32
  # no cache entry exists, send a new request.
35
33
  def find_via_cache(key, *arguments)
36
- result = CachedResource.cache.read(key).try(:dup)
37
- result && CachedResource.logger.info("#{CachedResource::Config::LOGGER_PREFIX} READ #{key} for #{arguments.inspect}")
34
+ result = cached_resource.cache.read(key).try(:dup)
35
+ result && cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} READ #{key} for #{arguments.inspect}")
38
36
  result || find_via_reload(key, *arguments)
39
37
  end
40
38
 
@@ -42,8 +40,8 @@ module CachedResource
42
40
  # for the request.
43
41
  def find_via_reload(key, *arguments)
44
42
  result = find_without_cache(*arguments)
45
- CachedResource.cache.write(key, result, :expires_in => CachedResource.config.cache_time_to_live)
46
- CachedResource.logger.info("#{CachedResource::Config::LOGGER_PREFIX} WRITE #{key} for #{arguments.inspect}")
43
+ cached_resource.cache.write(key, result, :expires_in => cached_resource.ttl)
44
+ cached_resource.logger.info("#{CachedResource::Configuration::LOGGER_PREFIX} WRITE #{key} for #{arguments.inspect}")
47
45
  result
48
46
  end
49
47
 
@@ -0,0 +1,42 @@
1
+ module CachedResource
2
+ # The Configuration class manages class specific options
3
+ # for cached resource.
4
+ class Configuration < OpenStruct
5
+
6
+ # default or fallback cache without rails
7
+ CACHE = ActiveSupport::Cache::MemoryStore.new
8
+
9
+ # default of fallback logger without rails
10
+ LOGGER = ActiveSupport::BufferedLogger.new(NilIO.new)
11
+
12
+ # prefix for log messages
13
+ LOGGER_PREFIX = "[cached_resource]"
14
+
15
+ # Initialize a Configuration with the given options, overriding any
16
+ # defaults. The following options exist for cached resource:
17
+ # :enabled, default: true
18
+ # :ttl, default: 604800
19
+ # :cache, default: Rails.cache or ActiveSupport::Cache::MemoryStore.new,
20
+ # :logger, default: Rails.logger or ActiveSupport::BufferedLogger.new(NilIO.new)
21
+ def initialize(options={})
22
+ super ({
23
+ :enabled => true,
24
+ :ttl => 604800,
25
+ :cache => defined?(Rails.cache) && Rails.cache || CACHE,
26
+ :logger => defined?(Rails.logger) && Rails.logger || LOGGER
27
+ }.merge(options))
28
+ end
29
+
30
+ # enable caching
31
+ def on!
32
+ self.enabled = true
33
+ end
34
+
35
+ # disable caching
36
+ def off!
37
+ self.enabled = false
38
+ end
39
+
40
+ end
41
+ end
42
+
@@ -0,0 +1,19 @@
1
+ module CachedResource
2
+ # NilIO emulates a null device (like /dev/null). This sort of
3
+ # doesn't belong here, but it's a dependency of cached resource.
4
+ class NilIO < StringIO
5
+
6
+ # Write to the null device. Disregards
7
+ # string parameter and returns the length
8
+ # of the string in bytes.
9
+ def write(string)
10
+ string.bytesize
11
+ end
12
+
13
+ # Read form the null device. Always returns nil.
14
+ def read(length=nil, buffer=nil)
15
+ nil
16
+ end
17
+
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module CachedResource
2
- VERSION = "1.0.1"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -5,6 +5,7 @@ describe CachedResource do
5
5
  before(:all) do
6
6
  class Thing < ActiveResource::Base
7
7
  self.site = "http://api.thing.com"
8
+ cached_resource
8
9
  end
9
10
 
10
11
  @thing = {:thing => {:id => 1, :name => "Ada"}}
@@ -13,12 +14,18 @@ describe CachedResource do
13
14
  @other_thing_json = @other_thing.to_json
14
15
  end
15
16
 
17
+ after(:all) do
18
+ Thing.cached_resource.cache.clear
19
+ Object.send(:remove_const, :Thing)
20
+ end
21
+
16
22
  describe "when enabled" do
17
23
 
18
- before(:all) do
24
+ before(:each) do
19
25
  # it's on by default, but lets call the method
20
26
  # to make sure it works
21
- CachedResource.on!
27
+ Thing.cached_resource.cache.clear
28
+ Thing.cached_resource.on!
22
29
 
23
30
  ActiveResource::HttpMock.reset!
24
31
  ActiveResource::HttpMock.respond_to do |mock|
@@ -28,23 +35,32 @@ describe CachedResource do
28
35
 
29
36
  it "should cache a response" do
30
37
  result = Thing.find(1)
31
- CachedResource.config.cache.read("thing/1").should == result
38
+ Thing.cached_resource.cache.read("thing/1").should == result
32
39
  end
33
40
 
34
41
  it "should read a response when the request is made again" do
42
+ # make a request
35
43
  Thing.find(1)
36
- # only one request should have been made by the test
37
- # before this one
44
+ # make the same request
45
+ Thing.find(1)
46
+ # only one request should have happened
38
47
  ActiveResource::HttpMock.requests.length.should == 1
39
48
  end
40
49
 
41
50
  it "should remake a request when reloaded" do
51
+ # make a request
52
+ Thing.find(1)
53
+ # make the same request, but reload it
42
54
  Thing.find(1, :reload => true)
55
+ # we should get two requests
43
56
  ActiveResource::HttpMock.requests.length.should == 2
44
57
  end
45
58
 
46
59
  it "should rewrite the cache when the request is reloaded" do
47
- old_result = CachedResource.config.cache.read("thing/1")
60
+ # make a request
61
+ Thing.find(1)
62
+ # get the cached result of the request
63
+ old_result = Thing.cached_resource.cache.read("thing/1")
48
64
 
49
65
  # change the response
50
66
  ActiveResource::HttpMock.reset!
@@ -53,18 +69,31 @@ describe CachedResource do
53
69
  end
54
70
 
55
71
  Thing.find(1, :reload => true)
56
- new_result = CachedResource.config.cache.read("thing/1")
72
+ new_result = Thing.cached_resource.cache.read("thing/1")
57
73
  # since active resources are equal if and only if they
58
74
  # are the same object or an instance of the same class,
59
75
  # not new?, and have the same id.
60
76
  new_result.name.should_not == old_result.name
61
77
  end
78
+
79
+ it "should remake the request when the ttl expires" do
80
+ # set cache time to live to 1 second
81
+ Thing.cached_resource.ttl = 1
82
+ # make a request
83
+ Thing.find(1)
84
+ # wait for the cache to expire
85
+ sleep(1.5)
86
+ # make the same request
87
+ Thing.find(1)
88
+ ActiveResource::HttpMock.requests.length.should == 2
89
+ end
62
90
  end
63
91
 
64
92
  describe "when disabled" do
65
93
 
66
- before(:all) do
67
- CachedResource.off!
94
+ before(:each) do
95
+ Thing.cached_resource.cache.clear
96
+ Thing.cached_resource.off!
68
97
 
69
98
  ActiveResource::HttpMock.reset!
70
99
  ActiveResource::HttpMock.respond_to do |mock|
@@ -74,18 +103,19 @@ describe CachedResource do
74
103
 
75
104
  it "should cache a response" do
76
105
  result = Thing.find(1)
77
- CachedResource.config.cache.read("thing/1").should == result
106
+ Thing.cached_resource.cache.read("thing/1").should == result
78
107
  end
79
108
 
80
109
  it "should always remake the request" do
81
110
  Thing.find(1)
82
- ActiveResource::HttpMock.requests.length.should == 2
111
+ ActiveResource::HttpMock.requests.length.should == 1
83
112
  Thing.find(1)
84
- ActiveResource::HttpMock.requests.length.should == 3
113
+ ActiveResource::HttpMock.requests.length.should == 2
85
114
  end
86
115
 
87
116
  it "should rewrite the cache for each request" do
88
- old_result = CachedResource.config.cache.read("thing/1")
117
+ Thing.find(1)
118
+ old_result = Thing.cached_resource.cache.read("thing/1")
89
119
 
90
120
  # change the response
91
121
  ActiveResource::HttpMock.reset!
@@ -94,7 +124,7 @@ describe CachedResource do
94
124
  end
95
125
 
96
126
  Thing.find(1)
97
- new_result = CachedResource.config.cache.read("thing/1")
127
+ new_result = Thing.cached_resource.cache.read("thing/1")
98
128
  # since active resources are equal if and only if they
99
129
  # are the same object or an instance of the same class,
100
130
  # not new?, and have the same id.
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe "CachedResource::Configuration" do
4
+
5
+ let(:configuration) { CachedResource::Configuration.new }
6
+
7
+ describe "by default" do
8
+ it "should be enabled" do
9
+ configuration.enabled.should == true
10
+ end
11
+
12
+ it "should have a cache expiry of 1 week" do
13
+ configuration.ttl.should == 604800
14
+ end
15
+
16
+ describe "outside a Rails environment" do
17
+ it "should be logging to a buffered logger attached to a NilIO" do
18
+ configuration.logger.class.should == ActiveSupport::BufferedLogger
19
+ configuration.logger.instance_variable_get(:@log).class.should == CachedResource::NilIO
20
+ end
21
+
22
+ it "should cache responses in a memory store" do
23
+ configuration.cache.class.should == ActiveSupport::Cache::MemoryStore
24
+ end
25
+ end
26
+
27
+ describe "inside a Rails environment" do
28
+ before(:each) do
29
+ Rails = OpenStruct.new(:logger => "logger", :cache => "cache")
30
+ load "cached_resource/configuration.rb"
31
+ end
32
+
33
+ after(:each) do
34
+ # remove the rails constant and unbind the
35
+ # cache and logger from the configuration
36
+ # defaults
37
+ Object.send(:remove_const, :Rails)
38
+ load "cached_resource/configuration.rb"
39
+ end
40
+
41
+ it "should be logging to the rails logger" do
42
+ configuration.logger.should == "logger"
43
+ end
44
+
45
+ it "should cache responses in a memory store" do
46
+ configuration.cache.should == "cache"
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "when initialized through cached resource" do
52
+ before(:each) do
53
+ class Foo < ActiveResource::Base
54
+ cached_resource :ttl => 1, :cache => "cache", :logger => "logger", :enabled => false, :custom => "irrelevant"
55
+ end
56
+ end
57
+
58
+ after(:each) do
59
+ Object.send(:remove_const, :Foo)
60
+ end
61
+
62
+ it "should relfect the specified options" do
63
+ Foo.cached_resource.ttl.should == 1
64
+ Foo.cached_resource.cache.should == "cache"
65
+ Foo.cached_resource.logger.should == "logger"
66
+ Foo.cached_resource.enabled.should == false
67
+ Foo.cached_resource.custom.should == "irrelevant"
68
+ end
69
+ end
70
+
71
+ describe "when multiple are initialized through cached resource" do
72
+ before(:each) do
73
+ class Foo < ActiveResource::Base
74
+ cached_resource
75
+ end
76
+
77
+ class Bar < ActiveResource::Base
78
+ cached_resource
79
+ end
80
+ end
81
+
82
+ after(:each) do
83
+ Object.send(:remove_const, :Foo)
84
+ Object.send(:remove_const, :Bar)
85
+ end
86
+
87
+ it "they should have different configuration objects" do
88
+ Foo.cached_resource.object_id.should_not == Bar.cached_resource.object_id
89
+ end
90
+
91
+ it "they should have the same cache" do
92
+ Foo.cached_resource.cache.should == Bar.cached_resource.cache
93
+ Foo.cached_resource.cache.object_id.should == Bar.cached_resource.cache.object_id
94
+ end
95
+
96
+ it "they should have the same ttl" do
97
+ Foo.cached_resource.ttl.should == Bar.cached_resource.ttl
98
+ end
99
+
100
+ it "they should have the same logger" do
101
+ Foo.cached_resource.logger.should == Bar.cached_resource.logger
102
+ Foo.cached_resource.logger.object_id.should == Bar.cached_resource.logger.object_id
103
+ end
104
+
105
+ it "they should have the same enablement" do
106
+ Foo.cached_resource.enabled.should == Bar.cached_resource.enabled
107
+ end
108
+
109
+ end
110
+
111
+
112
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe CachedResource::NilIO do
4
+ before(:all) do
5
+ @null_device = CachedResource::NilIO.new
6
+ end
7
+
8
+ it "should discard any data written" do
9
+ @null_device.write("I am writing something")
10
+ @null_device.readlines.should be_blank
11
+ end
12
+
13
+ it "should provide no data when read" do
14
+ @null_device.read(200).should == nil
15
+ end
16
+
17
+ it "should report EOF" do
18
+ @null_device.eof?.should == true
19
+ end
20
+ end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,5 @@ RSpec.configure do |config|
12
12
  # nada
13
13
  end
14
14
 
15
- # clear cache at beginning and end of execution
16
- CachedResource.cache.clear
17
- at_exit { CachedResource.cache.clear }
15
+ # change the logger so that we log to STDOUT
16
+ # CachedResource.logger = ActiveSupport::BufferedLogger.new(STDOUT)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_resource
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
- - 1
7
+ - 2
8
8
  - 0
9
9
  - 1
10
- version: 1.0.1
10
+ version: 2.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Chan
@@ -15,10 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-21 00:00:00 Z
18
+ date: 2011-11-17 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: activeresource
21
+ name: rake
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
@@ -32,7 +32,7 @@ dependencies:
32
32
  type: :runtime
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
- name: activesupport
35
+ name: activeresource
36
36
  prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
38
  none: false
@@ -46,7 +46,7 @@ dependencies:
46
46
  type: :runtime
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
49
- name: rspec
49
+ name: activesupport
50
50
  prerelease: false
51
51
  requirement: &id003 !ruby/object:Gem::Requirement
52
52
  none: false
@@ -57,8 +57,22 @@ dependencies:
57
57
  segments:
58
58
  - 0
59
59
  version: "0"
60
- type: :development
60
+ type: :runtime
61
61
  version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
62
76
  description: Enables request-based caching for ActiveResource
63
77
  email: email@suspi.net
64
78
  executables: []
@@ -76,10 +90,14 @@ files:
76
90
  - Rakefile
77
91
  - cached_resource.gemspec
78
92
  - lib/cached_resource.rb
93
+ - lib/cached_resource/cached_resource.rb
79
94
  - lib/cached_resource/caching.rb
80
- - lib/cached_resource/config.rb
95
+ - lib/cached_resource/configuration.rb
96
+ - lib/cached_resource/nilio.rb
81
97
  - lib/cached_resource/version.rb
82
98
  - spec/cached_resource/caching_spec.rb
99
+ - spec/cached_resource/configuration_spec.rb
100
+ - spec/cached_resource/nilio_spec.rb
83
101
  - spec/spec_helper.rb
84
102
  homepage: http://github.com/Ahsizara/cached_resource
85
103
  licenses: []
@@ -116,4 +134,6 @@ specification_version: 3
116
134
  summary: Caching for ActiveResource
117
135
  test_files:
118
136
  - spec/cached_resource/caching_spec.rb
137
+ - spec/cached_resource/configuration_spec.rb
138
+ - spec/cached_resource/nilio_spec.rb
119
139
  - spec/spec_helper.rb
@@ -1,29 +0,0 @@
1
- module CachedResource
2
- # The Config class is a singleton that contains
3
- # global configuration options for CacheResource
4
- class Config
5
- include Singleton
6
-
7
- # set default cache time to live to 1 week
8
- DEFAULT_CACHE_TIME_TO_LIVE = 604800
9
-
10
- # prefix for log messages
11
- LOGGER_PREFIX = "[cached_resource]"
12
-
13
- attr_accessor :cache_enabled, :cache_time_to_live, :logger, :cache
14
-
15
- # initialize the config with caching enabled and
16
- # a default cache expiry of 7 days. Also initializes
17
- # the logging and caching mechanisms, setting them to
18
- # the Rails logger and cache if available. If unavailable,
19
- # sets them to active support equivalents
20
- def initialize
21
- @cache_enabled = true
22
- @cache_time_to_live = DEFAULT_CACHE_TIME_TO_LIVE
23
-
24
- @cache = defined?(Rails.cache) && Rails.cache || ActiveSupport::Cache::MemoryStore.new
25
- @logger = defined?(Rails.logger) && Rails.logger || ActiveSupport::BufferedLogger.new(StringIO.new)
26
- end
27
-
28
- end
29
- end