cache_and_fetch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ require "cache_and_fetch/version"
2
+ require "active_support/concern"
3
+ require "active_support/core_ext/numeric/time"
4
+ require "rails"
5
+
6
+ module CacheAndFetch
7
+ end
8
+
9
+ require "cache_and_fetch/errors.rb"
10
+ require "cache_and_fetch/cacheable"
11
+ require "cache_and_fetch/fetchable"
@@ -0,0 +1,77 @@
1
+ module CacheAndFetch
2
+ module Cacheable
3
+ DEFAULT_CACHE_EXPIRY_TIME = 20.minutes
4
+ DEFAULT_PRIMARY_KEY_METHOD = :id
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ attr_accessor :cache_expires_at
10
+ unless self.respond_to?(:primary_key)
11
+ instance_eval do
12
+ def primary_key
13
+ self.primary_key = Cacheable::DEFAULT_PRIMARY_KEY_METHOD unless @primary_key
14
+ @primary_key
15
+ end
16
+
17
+ def primary_key=(val)
18
+ @primary_key = val
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def cache_duration
26
+ self.cache_duration = Cacheable::DEFAULT_CACHE_EXPIRY_TIME unless @cache_duration
27
+ @cache_duration
28
+ end
29
+
30
+ def cache_duration=(val)
31
+ @cache_duration = val
32
+ end
33
+
34
+ def cache_client
35
+ Rails.cache
36
+ end
37
+
38
+ def cache_key(p_key)
39
+ "#{self.name.underscore}/#{p_key}"
40
+ end
41
+
42
+ def cache(p_key)
43
+ resource = find(p_key)
44
+ resource.cache
45
+ resource
46
+ end
47
+
48
+ def get_cached(p_key)
49
+ cache_client.read(cache_key(p_key))
50
+ end
51
+ end
52
+
53
+ def cache_client
54
+ self.class.cache_client
55
+ end
56
+
57
+ def cache_key
58
+ self.class.cache_key(self.__send__(self.class.primary_key))
59
+ end
60
+
61
+ def cache
62
+ self.cache_expires_at = self.class.cache_duration.since.to_i
63
+ self.cache_client.write(self.cache_key, self)
64
+ end
65
+
66
+ def stale?
67
+ cache_expires_at && Time.now > Time.at(cache_expires_at)
68
+ end
69
+
70
+ def recache
71
+ Thread.new do
72
+ p_key = self.__send__(self.class.primary_key)
73
+ self.class.__send__(:find, p_key).cache
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,10 @@
1
+ module CacheAndFetch
2
+ class CacheAndFetchError < StandardError
3
+ end
4
+
5
+ class FinderNotFound < CacheAndFetchError
6
+ def initialize
7
+ super("There is no <model>.find method defined. Please implement a <model>.find method or a finder module in app/models/<model>/finder.rb")
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ module CacheAndFetch
2
+ module Fetchable
3
+ extend ActiveSupport::Concern
4
+ include Cacheable
5
+
6
+ included do
7
+ begin
8
+ extend "#{name}::Finder".constantize
9
+ rescue NameError => ex
10
+ raise FinderNotFound.new unless respond_to?(:find)
11
+ end
12
+ private_class_method :find
13
+ end
14
+
15
+ module ClassMethods
16
+ def fetch(p_key)
17
+ resource = get_cached(p_key)
18
+ if resource
19
+ if resource.stale?
20
+ block_given? ? yield(resource) : resource.recache
21
+ end
22
+ else
23
+ resource = cache(p_key)
24
+ end
25
+ resource
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module CacheAndFetch
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_and_fetch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Suman Mukherjee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A gem that allows you to soft expire cache, use a stale resource and
63
+ fetch the fresh resource in background
64
+ email:
65
+ - sumanmukherjee03@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/cache_and_fetch/cacheable.rb
71
+ - lib/cache_and_fetch/errors.rb
72
+ - lib/cache_and_fetch/fetchable.rb
73
+ - lib/cache_and_fetch/version.rb
74
+ - lib/cache_and_fetch.rb
75
+ homepage: http://github.com/sumanmukherjee03/cache_and_fetch
76
+ licenses:
77
+ - MIT
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: -3505621410664163532
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: -3505621410664163532
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.25
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: This gem allows you to add a soft expiry to your cache. When the cache soft
106
+ expires, the cache provides you the stale record to work with, but fetches the fresh
107
+ record in the background.
108
+ test_files: []