smart_cache 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gemspec
2
+ pkg
3
+ Gemfile.lock
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@caching --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'rubygems'
@@ -0,0 +1,86 @@
1
+ module ActiveRecord
2
+
3
+ class Base
4
+ include SmartCache
5
+ EXPIRE_TIME = 10.minutes
6
+
7
+ def update_attributes(attributes)
8
+ if self.frozen?
9
+ @attributes = @attributes.dup
10
+ end
11
+ super
12
+ end
13
+
14
+ def update_attributes!(attributes)
15
+ if self.frozen?
16
+ @attributes = @attributes.dup
17
+ end
18
+ super
19
+ end
20
+ end
21
+
22
+ module FinderMethods
23
+
24
+ require 'digest/md5'
25
+
26
+ def find(*args)
27
+ return to_a.find { |*block_args| yield(*block_args) } if block_given?
28
+
29
+ options = args.extract_options!
30
+
31
+ if options.present?
32
+ apply_finder_options(options).find(*args)
33
+ else
34
+ case args.first
35
+ when :first, :last, :all
36
+ send(args.first)
37
+ else
38
+ if args.length==1
39
+ #puts @klass.name
40
+ cache_key = Digest::MD5.hexdigest("short_cache_by_id_#{@klass.name}_#{args[0]}")
41
+ results = Rails.cache.read(cache_key)
42
+ return results if results
43
+ results = find_with_ids(*args)
44
+ Rails.cache.write(cache_key, results, :expires_in => eval("#{@klass.name}::EXPIRE_TIME"))
45
+ return results
46
+ end
47
+ find_with_ids(*args)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ class Relation
54
+ require 'digest/md5'
55
+
56
+ def clear_smart_cache
57
+ cache_key = Digest::MD5.hexdigest(arel.to_sql)
58
+ Rails.cache.write(cache_key, nil, :expires_in => 0.seconds)
59
+ end
60
+
61
+ def smart_cache(ttl = nil)
62
+ return @records if loaded?
63
+ cache_key = Digest::MD5.hexdigest(arel.to_sql)
64
+ results = Rails.cache.read(cache_key)
65
+ return results if results
66
+ @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel.to_sql)
67
+
68
+ preload = @preload_values
69
+ preload += @includes_values unless eager_loading?
70
+ preload.each {|associations| @klass.send(:preload_associations, @records, associations) }
71
+
72
+ # @readonly_value is true only if set explicitly. @implicit_readonly is true if there
73
+ # are JOINS and no explicit SELECT.
74
+ readonly = @readonly_value.nil? ? @implicit_readonly : @readonly_value
75
+ @records.each { |record| record.readonly! } if readonly
76
+
77
+ @loaded = true
78
+ # key_list = Rails.cache.read("short_cache_#{@klass.name}")
79
+ # key_list = [] if key_list.nil?
80
+ # key_list.push cache_key
81
+ # Rails.cache.write("short_cache_#{@klass.name}", key_list)
82
+ Rails.cache.write(cache_key, @records, :expires_in => eval("ttl || #{@klass.name}::EXPIRE_TIME"))
83
+ @records
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,25 @@
1
+ module SmartCache
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ def self.included(base)
6
+ base.after_save :update_caching
7
+ end
8
+
9
+ def self.block_cache(key=nil, ttl=1.day)
10
+ results = Rails.cache.read(key)
11
+ return results if results
12
+ results = yield
13
+ Rails.cache.write(key, results, :expires_in => ttl)
14
+ return results
15
+ end
16
+
17
+ def update_caching
18
+ cache_key = Digest::MD5.hexdigest("short_cache_by_id_#{self.class.name}_#{self.id}")
19
+ Rails.cache.write(cache_key, self, :expires_in => eval("#{self.class.name}::EXPIRE_TIME"))
20
+ return true
21
+ end
22
+
23
+ end
24
+
25
+ require 'smart_cache/active_record.rb'
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+
4
+ #written within a test rails application
5
+ class SmartCacheTest < ActiveSupport::TestCase
6
+
7
+ require 'digest/md5'
8
+
9
+ test "long caching data" do
10
+ time_to_live = 1.day
11
+ product = SmartCache.block_cache("product_first", time_to_live) do
12
+ Product.first
13
+ end
14
+ assert product.is_a?(Product), "Returned item is not a Product"
15
+ assert Rails.cache.read("product_first").is_a?(Product)
16
+ assert_equal "Product Two", product.name
17
+
18
+ end
19
+
20
+ test "caching data" do
21
+ cart = Cart.order(:id).smart_cache.first
22
+ cart2 = Rails.cache.read(Digest::MD5.hexdigest(Cart.order(:id).to_sql)).first
23
+ assert cart.is_a?(Cart), "Returned item is not cart"
24
+ assert_equal cart2, cart
25
+ end
26
+
27
+ test "standard cache find" do
28
+ cart = Cart.find(Cart.first.id)
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_cache
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Kelly Mahan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-14 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.0.5
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Smart Cache
28
+ email: kmahan@kmahan.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - .rvmrc
38
+ - Gemfile
39
+ - README.md
40
+ - Rakefile
41
+ - lib/simple_cache/active_record.rb
42
+ - lib/smart_cache.rb
43
+ - test/unit/smart_cache_test.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/kellymahan/SmartCache
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: smart_caching
68
+ rubygems_version: 1.5.2
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Smart Cache
72
+ test_files:
73
+ - test/unit/smart_cache_test.rb