cachetastic 2.1.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/LICENSE +21 -0
  2. data/README +57 -43
  3. data/doc/classes/Cachetastic/Adapters.html +180 -0
  4. data/doc/classes/Cachetastic/Adapters/Base.html +206 -123
  5. data/doc/classes/Cachetastic/Adapters/File.html +17 -130
  6. data/doc/classes/Cachetastic/Adapters/LocalMemory.html +7 -228
  7. data/doc/classes/Cachetastic/Adapters/Memcached.html +193 -0
  8. data/doc/classes/Cachetastic/Cache.html +425 -0
  9. data/doc/classes/Cachetastic/Cacheable.html +26 -30
  10. data/doc/classes/Cachetastic/Cacheable/ClassAndInstanceMethods.html +66 -62
  11. data/doc/classes/Cachetastic/Cacheable/ClassOnlyMethods.html +30 -30
  12. data/doc/classes/Cachetastic/Logger.html +31 -33
  13. data/doc/created.rid +1 -1
  14. data/doc/files/{lib/cachetastic/adapters/html_file_rb.html → LICENSE.html} +29 -34
  15. data/doc/files/README.html +91 -57
  16. data/doc/files/lib/cachetastic/adapters/base_rb.html +1 -39
  17. data/doc/files/lib/cachetastic/adapters/file_rb.html +1 -21
  18. data/doc/files/lib/cachetastic/adapters/local_memory_rb.html +1 -9
  19. data/doc/files/lib/cachetastic/{ruby_extensions/kernel_rb.html → adapters/memcached_rb.html} +4 -4
  20. data/doc/files/lib/cachetastic/{ruby_extensions/string_rb.html → cache_rb.html} +5 -5
  21. data/doc/files/lib/cachetastic/cacheable_rb.html +1 -1
  22. data/doc/files/lib/cachetastic/{caches/base_rb.html → extensions/string_rb.html} +5 -5
  23. data/doc/files/lib/cachetastic/logger_rb.html +1 -7
  24. data/doc/files/lib/cachetastic/{adapters/store_object_rb.html → store_object_rb.html} +3 -3
  25. data/doc/files/lib/cachetastic_rb.html +4 -95
  26. data/doc/fr_class_index.html +3 -15
  27. data/doc/fr_file_index.html +5 -16
  28. data/doc/fr_method_index.html +26 -78
  29. data/lib/cachetastic.rb +16 -70
  30. data/lib/cachetastic/adapters/base.rb +178 -76
  31. data/lib/cachetastic/adapters/file.rb +63 -46
  32. data/lib/cachetastic/adapters/local_memory.rb +36 -67
  33. data/lib/cachetastic/adapters/memcached.rb +114 -0
  34. data/lib/cachetastic/cache.rb +165 -0
  35. data/lib/cachetastic/cacheable.rb +19 -15
  36. data/lib/cachetastic/extensions/string.rb +8 -0
  37. data/lib/cachetastic/logger.rb +41 -41
  38. data/lib/cachetastic/store_object.rb +22 -0
  39. metadata +43 -64
  40. data/bin/cachetastic_drb_server +0 -115
  41. data/doc/classes/ActiveRecord/Base.html +0 -194
  42. data/doc/classes/CGI/Session/CachetasticStore.html +0 -124
  43. data/doc/classes/Cachetastic/Adapters/Drb.html +0 -332
  44. data/doc/classes/Cachetastic/Adapters/FileBase.html +0 -309
  45. data/doc/classes/Cachetastic/Adapters/HtmlFile.html +0 -224
  46. data/doc/classes/Cachetastic/Adapters/Memcache.html +0 -498
  47. data/doc/classes/Cachetastic/Caches/Base.html +0 -643
  48. data/doc/classes/Cachetastic/Caches/Base/RegisteredCaches.html +0 -179
  49. data/doc/classes/Cachetastic/Caches/MackSessionCache.html +0 -119
  50. data/doc/classes/Cachetastic/Caches/PageCache.html +0 -121
  51. data/doc/classes/Cachetastic/Caches/RailsSessionCache.html +0 -154
  52. data/doc/classes/Cachetastic/Connection.html +0 -212
  53. data/doc/classes/Cachetastic/Errors/UnsupportedAdapter.html +0 -146
  54. data/doc/classes/Object.html +0 -222
  55. data/doc/classes/String.html +0 -172
  56. data/doc/files/lib/cachetastic/adapters/drb_rb.html +0 -115
  57. data/doc/files/lib/cachetastic/adapters/file_base_rb.html +0 -109
  58. data/doc/files/lib/cachetastic/adapters/memcache_rb.html +0 -127
  59. data/doc/files/lib/cachetastic/caches/mack_session_cache_rb.html +0 -107
  60. data/doc/files/lib/cachetastic/caches/page_cache_rb.html +0 -109
  61. data/doc/files/lib/cachetastic/caches/rails_session_cache_rb.html +0 -107
  62. data/doc/files/lib/cachetastic/connection_rb.html +0 -107
  63. data/doc/files/lib/cachetastic/errors/unsupported_adapter_rb.html +0 -101
  64. data/doc/files/lib/cachetastic/rails_extensions/active_record_base_rb.html +0 -101
  65. data/doc/files/lib/cachetastic/rails_extensions/cgi_session_store_rb.html +0 -109
  66. data/doc/files/lib/cachetastic/ruby_extensions/object_rb.html +0 -101
  67. data/lib/cachetastic/adapters/drb.rb +0 -51
  68. data/lib/cachetastic/adapters/file_base.rb +0 -86
  69. data/lib/cachetastic/adapters/html_file.rb +0 -68
  70. data/lib/cachetastic/adapters/memcache.rb +0 -114
  71. data/lib/cachetastic/adapters/store_object.rb +0 -28
  72. data/lib/cachetastic/caches/base.rb +0 -238
  73. data/lib/cachetastic/caches/mack_session_cache.rb +0 -3
  74. data/lib/cachetastic/caches/page_cache.rb +0 -6
  75. data/lib/cachetastic/caches/rails_session_cache.rb +0 -12
  76. data/lib/cachetastic/connection.rb +0 -24
  77. data/lib/cachetastic/errors/unsupported_adapter.rb +0 -7
  78. data/lib/cachetastic/rails_extensions/active_record_base.rb +0 -24
  79. data/lib/cachetastic/rails_extensions/cgi_session_store.rb +0 -59
  80. data/lib/cachetastic/ruby_extensions/kernel.rb +0 -25
  81. data/lib/cachetastic/ruby_extensions/object.rb +0 -22
  82. data/lib/cachetastic/ruby_extensions/string.rb +0 -15
  83. data/test/active_record_test.rb +0 -89
  84. data/test/cacheable_test.rb +0 -88
  85. data/test/cachetastic_unit_test.rb +0 -74
  86. data/test/config.rb +0 -30
  87. data/test/drb_adapter_test.rb +0 -14
  88. data/test/file_adapter_test.rb +0 -49
  89. data/test/memcache_adapter_test.rb +0 -18
  90. data/test/test_helper.rb +0 -75
@@ -1,3 +0,0 @@
1
- # Used for storing Mack sessions.
2
- class Cachetastic::Caches::MackSessionCache < Cachetastic::Caches::Base
3
- end
@@ -1,6 +0,0 @@
1
- # Used by the Mongrel Page Cache Handler gem for page caching.
2
- # Future functionality may be added to this class to allow for
3
- # things such as deleting a certain page/set of pages based on
4
- # regex, etc...
5
- class Cachetastic::Caches::PageCache < Cachetastic::Caches::Base
6
- end
@@ -1,12 +0,0 @@
1
- # Used for storing Rails sessions.
2
- class Cachetastic::Caches::RailsSessionCache < Cachetastic::Caches::Base
3
-
4
- class << self
5
-
6
- def unsupported_adapters
7
- [Cachetastic::Adapters::File]
8
- end
9
-
10
- end
11
-
12
- end
@@ -1,24 +0,0 @@
1
- # This class caches adapter objects for each cache in the system.
2
- class Cachetastic::Connection
3
- include Singleton
4
-
5
- attr_accessor :connections
6
-
7
- def initialize
8
- self.connections = {}
9
- end
10
-
11
- # Takes the name of the cache, that's been methodized, and returns back the adapter object associated with the cache.
12
- # If the adapter object doesn't exist or if the adapter is no longer valid (adapter.valid?) a new one is
13
- # created and returned.
14
- def get(name)
15
- name = name.to_sym
16
- conn = self.connections[name]
17
- return conn if conn && conn.valid?
18
- adapter = Cachetastic::Adapters::Base.configuration(name).adapter.to_s.camelcase
19
- conn = "Cachetastic::Adapters::#{adapter}".constantize.new(name)
20
- self.connections[name.to_sym] = conn
21
- return conn
22
- end
23
-
24
- end
@@ -1,7 +0,0 @@
1
- class Cachetastic::Errors::UnsupportedAdapter < Exception
2
-
3
- def initialize(cache_name, adapter)
4
- super("#{cache_name} does not support the use of the #{adapter} adapter!")
5
- end
6
-
7
- end
@@ -1,24 +0,0 @@
1
- class ActiveRecord::Base
2
-
3
- include Cachetastic::Cacheable
4
-
5
- def cachetastic_key
6
- self.id
7
- end
8
-
9
- # Returns an object from the cache for a given key.
10
- # If the object returned is nil and the self_populate parameter is true
11
- # then the key will be used to try and find the object in the database,
12
- # set the object into the cache, and then return the object.
13
- def self.get_from_cache(key, self_populate = false)
14
- res = cache_class.get(key)
15
- if res.nil? && self_populate
16
- res = self.name.constantize.find(key)
17
- unless res.nil?
18
- res.cache_self
19
- end
20
- end
21
- res
22
- end
23
-
24
- end
@@ -1,59 +0,0 @@
1
- require 'cgi'
2
- require 'cgi/session'
3
-
4
- class CGI #:nodoc:#
5
- class Session #:nodoc:#
6
- # Allows Rails to use Cachetastic for it's session store.
7
- # The setting below needs to happen AFTER the gem has been required, obviously!
8
- # In Rails 1.2.6 this is AFTER the initializer block.
9
- # ActionController::Base.session_store = :cachetastic_store
10
- class CachetasticStore
11
-
12
-
13
- def check_id(id) #:nodoc:#
14
- /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true
15
- end
16
-
17
- def initialize(session, options = {}) #:nodoc:#
18
- id = session.session_id
19
- unless check_id(id)
20
- raise ArgumentError, "session_id '%s' is invalid" % id
21
- end
22
-
23
- @session_key = id
24
-
25
- @session_data = {}
26
- end
27
-
28
- # Restore session state from the session's memcache entry.
29
- #
30
- # Returns the session state as a hash.
31
- def restore #:nodoc:#
32
- @session_data = Cachetastic::Caches::RailsSessionCache.get(@session_key) do
33
- {}
34
- end
35
- end
36
-
37
- # Save session state to the session's memcache entry.
38
- def update #:nodoc:#
39
- Cachetastic::Caches::RailsSessionCache.set(@session_key, @session_data)
40
- end
41
-
42
- # Update and close the session's memcache entry.
43
- def close #:nodoc:#
44
- update
45
- end
46
-
47
- # Delete the session's memcache entry.
48
- def delete #:nodoc:#
49
- Cachetastic::Caches::RailsSessionCache.delete(@session_key)
50
- @session_data = {}
51
- end
52
-
53
- def data #:nodoc:#
54
- @session_data
55
- end
56
-
57
- end
58
- end
59
- end
@@ -1,25 +0,0 @@
1
- module Kernel # :nodoc:
2
-
3
- def retryable(options = {}, &block) # :nodoc:
4
- opts = { :tries => 1, :on => Exception }.merge(options)
5
-
6
- retries = opts[:tries]
7
- retry_exceptions = [opts[:on]].flatten
8
-
9
- x = %{
10
- begin
11
- return yield
12
- rescue #{retry_exceptions.join(", ")} => e
13
- retries -= 1
14
- if retries > 0
15
- retry
16
- else
17
- raise e
18
- end
19
- end
20
- }
21
-
22
- eval(x, &block)
23
- end
24
-
25
- end
@@ -1,22 +0,0 @@
1
- class Object
2
-
3
- # Override this method in your object if you use the Cachetastic::Cacheable module.
4
- def cachetastic_key
5
- raise NoMethodError.new('cachetastic_key')
6
- end
7
-
8
- unless respond_to?(:ivar_cache)
9
- def ivar_cache(var_name = nil, &block)
10
- if var_name.nil?
11
- call = caller[0]
12
- var_name = call[(call.index('`')+1)...call.index("'")]
13
- end
14
- var = instance_variable_get("@#{var_name}")
15
- unless var
16
- return instance_variable_set("@#{var_name}", yield) if block_given?
17
- end
18
- instance_variable_get("@#{var_name}")
19
- end
20
- end
21
-
22
- end
@@ -1,15 +0,0 @@
1
- class String
2
-
3
- unless ''.respond_to?(:camelcase)
4
- def camelcase
5
- to_s.gsub(/\/(.?)/){ "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/){ $1.upcase }
6
- end
7
- end
8
-
9
- unless ''.respond_to?(:constantize)
10
- def constantize
11
- Module.instance_eval("::#{self}")
12
- end
13
- end
14
-
15
- end
@@ -1,89 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class ActiveRecordTest < Test::Unit::TestCase
4
-
5
- def setup
6
- ArAlbumCache.expire_all
7
- Cachetastic::Caches::Base.expire_all
8
- #FileUtils.rm_f(AR_DB, :verbose => true)# if File.exists?(AR_DB)
9
- begin
10
- ArMigration.down
11
- rescue Exception => e
12
- end
13
- ArMigration.up
14
- a = ArAlbum.create(:title => "Abbey Road", :artist => "The Beatles")
15
- ArSong.create(:title => "Come Together", :album_id => a.id)
16
- ArSong.create(:title => "Something", :album_id => a.id)
17
- a = ArAlbum.create(:title => "Help!", :artist => "The Beatles")
18
- ArSong.create(:title => "Yesterday", :album_id => a.id)
19
- ArSong.create(:title => "Dizzy Miss Lizzie", :album_id => a.id)
20
- end
21
-
22
- def teardown
23
- ArMigration.down
24
- #FileUtils.rm_f(AR_DB, :verbose => true)# if File.exists?(AR_DB)
25
- end
26
-
27
- def test_album_cache
28
- assert true
29
- res = ArAlbumCache.get(:recent_albums, 5)
30
- assert_not_nil res
31
- assert res.is_a?(Array)
32
- assert_equal 5, res.size
33
- res.each do |r|
34
- assert r.is_a?(ArAlbum)
35
- end
36
- end
37
-
38
- def test_extensions
39
- album = ArAlbum.find(1)
40
- # assert !Cachetastic::Cacheable.const_defined?("ArAlbumCache")
41
- album.cache_self
42
- # assert Cachetastic::Cacheable.const_defined?("ArAlbumCache")
43
- assert_equal album, Cachetastic::Cacheable::ArAlbumCache.get(1)
44
-
45
- song = ArSong.find(1)
46
- # assert !Cachetastic::Cacheable.const_defined?("ArSongCache")
47
- song.cache_self
48
- # assert Cachetastic::Cacheable.const_defined?("ArSongCache")
49
- assert_equal song, Cachetastic::Cacheable::ArSongCache.get(1)
50
- assert_equal album, Cachetastic::Cacheable::ArAlbumCache.get(1)
51
-
52
- song.uncache_self
53
- assert_nil Cachetastic::Cacheable::ArSongCache.get(1)
54
-
55
- ac = ArAlbum.get_from_cache(1)
56
- assert_not_nil ac
57
- assert_equal album, ac
58
-
59
- ArAlbum.delete_from_cache(1)
60
- ac = ArAlbum.get_from_cache(1)
61
- assert_nil ac
62
-
63
- ac = ArAlbum.set_into_cache(1, album)
64
- assert_not_nil ac
65
- assert_equal album, ac
66
-
67
- ac = ArAlbum.cacher(:foobar) do
68
- ArAlbum.find(2)
69
- end
70
-
71
- assert_not_nil ac
72
- assert_equal ArAlbum.find(2), ac
73
-
74
- ac = ArAlbum.get_from_cache(:foobar)
75
- assert_not_nil ac
76
- assert_equal ArAlbum.find(2), ac
77
-
78
- assert_equal [1,2,3,:yippie], ac.some_numbers([1,2,3])
79
- assert_equal [1,2,3,:yippie], ArAlbum.get_from_cache(:some_numbers)
80
- assert_equal [1,2,3,:yippie], ac.some_numbers([1,2,3,4,5,6])
81
-
82
- ArAlbum.delete_from_cache(1)
83
- ac = ArAlbum.get_from_cache(1)
84
- assert_nil ac
85
- ac = ArAlbum.get_from_cache(1, true)
86
- assert_not_nil ac
87
- end
88
-
89
- end
@@ -1,88 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class Person
4
- include Cachetastic::Cacheable
5
-
6
- attr_accessor :name
7
-
8
- def cachetastic_key
9
- self.name
10
- end
11
-
12
- def always_the_same(x, y)
13
- cacher("always_the_same") do
14
- x + y
15
- end
16
- end
17
-
18
- end
19
-
20
- module Me
21
- class You
22
- include Cachetastic::Cacheable
23
- end
24
- end
25
-
26
- class Unknown
27
- include Cachetastic::Cacheable
28
- end
29
-
30
- class CacheableTest < Test::Unit::TestCase
31
-
32
- def test_cache_classes_work_with_modules
33
- assert !Cachetastic::Cacheable.const_defined?("Me_YouCache")
34
- assert Me::You.cache_class == Cachetastic::Cacheable::Me_YouCache
35
- assert Cachetastic::Cacheable.const_defined?("Me_YouCache")
36
- end
37
-
38
- def test_cache_classes_are_auto_genned
39
- assert !Cachetastic::Cacheable.const_defined?("PersonCache")
40
- assert Person.cache_class == Cachetastic::Cacheable::PersonCache
41
- assert Cachetastic::Cacheable.const_defined?("PersonCache")
42
- assert !Cachetastic::Cacheable.const_defined?("UnknownCache")
43
- assert Unknown.cache_class == Cachetastic::Cacheable::UnknownCache
44
- assert Cachetastic::Cacheable.const_defined?("UnknownCache")
45
- end
46
-
47
- def test_no_cachetastic_key
48
- assert_raise(NoMethodError) { Unknown.new.cachetastic_key }
49
- assert_raise(NoMethodError) { Unknown.new.cache_self }
50
- assert_raise(NoMethodError) { Unknown.new.uncache_self }
51
- end
52
-
53
- def test_cache_uncache_self
54
- p = Person.new
55
- p.name = "Mark Bates"
56
- assert_nil Cachetastic::Cacheable::PersonCache.get("Mark Bates")
57
- p.cache_self
58
- assert_equal p, Cachetastic::Cacheable::PersonCache.get("Mark Bates")
59
- p.uncache_self
60
- assert_nil Cachetastic::Cacheable::PersonCache.get("Mark Bates")
61
- end
62
-
63
- def test_cacher
64
- p = Person.new
65
- assert_nil Cachetastic::Cacheable::PersonCache.get("always_the_same")
66
- assert_equal 4, p.always_the_same(1,3)
67
- assert_equal 4, Cachetastic::Cacheable::PersonCache.get("always_the_same")
68
- assert_equal 4, p.always_the_same(99,99)
69
- assert_equal 4, Person.get_from_cache("always_the_same")
70
- assert_nil Cachetastic::Cacheable::PersonCache.get("say_hi")
71
- Person.cacher("say_hi") do
72
- "hi there"
73
- end
74
- assert_equal "hi there", Cachetastic::Cacheable::PersonCache.get("say_hi")
75
- assert_equal "hi there", Person.get_from_cache("say_hi")
76
- assert_equal "hi there", Person.cacher("say_hi")
77
- end
78
-
79
- def test_get_from_cache
80
- assert_nil Person.get_from_cache("i should be nil")
81
- assert_equal 86, Person.get_from_cache("maxwell smart") {86}
82
- x = Person.get_from_cache("my name") do |key|
83
- "Mark Bates"
84
- end
85
- assert_equal "Mark Bates", x
86
- end
87
-
88
- end
@@ -1,74 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class CachetasticUnitTest < Test::Unit::TestCase
4
-
5
-
6
- def setup
7
- Cachetastic::Caches::Base.expire_all
8
- end
9
-
10
- def test_class_cannot_be_instaniate
11
- assert_raise(NoMethodError) { Cachetastic::Caches::Base.new }
12
- end
13
-
14
- def test_get
15
- assert_nil Cachetastic::Caches::Base.get(:my_articles_test_get)
16
- res = Cachetastic::Caches::Base.get(:my_articles_test_get) do
17
- "hello mark"
18
- end
19
- assert_equal "hello mark", res
20
- assert_nil Cachetastic::Caches::Base.get(:my_articles_test_get)
21
-
22
- res = Cachetastic::Caches::Base.get(:my_articles_test_get) do
23
- Cachetastic::Caches::Base.set(:my_articles_test_get, "hello mark")
24
- end
25
- assert_equal "hello mark", res
26
- assert_not_nil Cachetastic::Caches::Base.get(:my_articles_test_get)
27
- assert_equal "hello mark", Cachetastic::Caches::Base.get(:my_articles_test_get)
28
- end
29
-
30
- def test_set
31
- assert true
32
- Cachetastic::Caches::Base.set(:my_articles, [1, 2, 3], 15.minutes.from_now)
33
- assert_equal [1,2,3], Cachetastic::Caches::Base.get(:my_articles)
34
- end
35
-
36
- def test_delete
37
- test_set
38
- Cachetastic::Caches::Base.delete(:my_articles)
39
- assert_nil Cachetastic::Caches::Base.get(:my_articles)
40
- end
41
-
42
- def test_expire_all
43
- a = [1,2,3,4,5,6,7,8,9,10]
44
- Cachetastic::Caches::Base.set(:testing_expire_all, a)
45
- assert_equal a, Cachetastic::Caches::Base.get(:testing_expire_all)
46
- Cachetastic::Caches::Base.expire_all
47
- assert_nil Cachetastic::Caches::Base.get(:testing_expire_all)
48
- end
49
-
50
- def test_local_memory_auto_expire
51
- res = FooBarCache.get(:pickles)
52
- assert_nil res
53
- FooBarCache.set(:pickles, "are yummy")
54
- res = FooBarCache.get(:pickles)
55
- assert_not_nil res
56
- assert_equal "are yummy", res
57
- sleep(4)
58
- res = FooBarCache.get(:pickles)
59
- assert_nil res
60
- end
61
-
62
- def test_local_memory_delete
63
- res = FooBarCache.get(:pickles)
64
- assert_nil res
65
- FooBarCache.set(:pickles, "are yummy")
66
- res = FooBarCache.get(:pickles)
67
- assert_not_nil res
68
- assert_equal "are yummy", res
69
- FooBarCache.delete(:pickles)
70
- res = FooBarCache.get(:pickles)
71
- assert_nil res
72
- end
73
-
74
- end