localmemcache_store 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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Florian Dütsch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ NOT READY YET! COME BACK IN SOME DAYS.
2
+
3
+
4
+ LocalmemcacheStore
5
+ ==================
6
+
7
+ Introduction goes here.
8
+
9
+
10
+ Example
11
+ =======
12
+
13
+ Example goes here.
14
+
15
+
16
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ require 'rubygems'
6
+ require 'rake/gempackagetask'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the localmemcache_store plugin.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ desc 'Generate documentation for the localmemcache_store plugin.'
20
+ Rake::RDocTask.new(:rdoc) do |rdoc|
21
+ rdoc.rdoc_dir = 'rdoc'
22
+ rdoc.title = 'LocalmemcacheStore'
23
+ rdoc.options << '--line-numbers' << '--inline-source'
24
+ rdoc.rdoc_files.include('README')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
27
+
28
+ ################################################################################
29
+ # GEM stuff
30
+
31
+ PKG_FILES = FileList[
32
+ '[a-zA-Z]*',
33
+ # 'generators/**/*',
34
+ 'lib/**/*',
35
+ 'rails/**/*',
36
+ 'tasks/**/*',
37
+ 'test/**/*'
38
+ ]
39
+
40
+ spec = Gem::Specification.new do |s|
41
+ s.name = "localmemcache_store"
42
+ s.version = "0.0.1"
43
+ s.author = "Florian Dütsch (der_flo)"
44
+ s.email = "mail@florian-duetsch.de"
45
+ s.homepage = "http://github.com/der-flo/localmemcache_store"
46
+ s.platform = Gem::Platform::RUBY
47
+ s.summary = "A Rails cache store implementation for localmemcache"
48
+ s.files = PKG_FILES.to_a
49
+ s.require_path = "lib"
50
+ s.has_rdoc = false
51
+ s.extra_rdoc_files = ["README"]
52
+ end
53
+
54
+ desc 'Turn this plugin into a gem.'
55
+ Rake::GemPackageTask.new(spec) do |pkg|
56
+ pkg.gem_spec = spec
57
+ end
data/TODOs.txt ADDED
@@ -0,0 +1,15 @@
1
+ Plugin
2
+ * Better / more complete tests
3
+ * Gemified version etc: http://guides.rubyonrails.org/plugins.html
4
+ * Good default parameters for localmemcache configuration
5
+ * Documentation! (README, Homepage, ...)
6
+ * Some Rake tasks?
7
+ * Statistics
8
+ ** Controller generator?
9
+ ** Via Rails Engines?
10
+
11
+ Adjustment proposals for localmemcache
12
+ * Do expiry handling inside the cache
13
+ * Drop old entries instead of raising OutOfMemoryError
14
+ * Add statistics: How much used / free memory?
15
+ * delete_matched: Delete entries by regex
@@ -0,0 +1,75 @@
1
+ begin
2
+ gem 'localmemcache', '>=0.4.3'
3
+ require 'localmemcache'
4
+ rescue LoadError
5
+ raise '"localmemcache>=0.4.3" gem is not installed!'
6
+ end
7
+
8
+ module ActiveSupport::Cache
9
+ class LocalmemcacheStore < Store
10
+
11
+ DataExpiresPair = Struct.new(:data, :expires_at)
12
+
13
+ def initialize options = {}
14
+ # TODO: Define default parameters
15
+ options.reverse_merge!({
16
+ :namespace => :x,
17
+ :size_mb => 64
18
+ })
19
+ @lmc = LocalMemCache::SharedObjectStorage.new options
20
+ end
21
+
22
+ def read(name, options = nil)
23
+ super
24
+ data_expires_pair = @lmc[name]
25
+ return nil unless data_expires_pair
26
+
27
+ # entry expired?
28
+ if data_expires_pair.expires_at &&
29
+ data_expires_pair.expires_at <= Time.now
30
+
31
+ # delete entry from database
32
+ @lmc.delete(name)
33
+ nil
34
+ else
35
+ data_expires_pair.data
36
+ end
37
+ end
38
+
39
+ def write(name, value, options = {})
40
+ super
41
+ data = value.freeze
42
+ expires_in = options[:expires_in]
43
+ expires_at = if expires_in && expires_in.to_i > 0
44
+ Time.now + expires_in.to_i
45
+ end
46
+ @lmc[name] = DataExpiresPair.new(data, expires_at)
47
+ data
48
+ end
49
+
50
+ def delete(name, options = nil)
51
+ super
52
+ @lmc.delete(name)
53
+ end
54
+
55
+ def delete_matched(matcher, options = nil)
56
+ super
57
+ # TODO: Performance?
58
+ @lmc.each_pair do |key, value|
59
+ next unless key =~ matcher
60
+ @lmc.delete(key)
61
+ end
62
+ end
63
+
64
+ def exist?(name,options = nil)
65
+ super
66
+ # TODO: Performance?
67
+ # Read the value and check for nil?
68
+ @lmc.keys.include?(name)
69
+ end
70
+
71
+ def clear
72
+ @lmc.clear
73
+ end
74
+ end
75
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'active_support/cache/localmemcache_store'
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :localmemcache_store do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,62 @@
1
+ # Module borrowed from the Rails tests
2
+ # http://github.com/rails/rails/blob/master/activesupport/test/caching_test.rb
3
+
4
+ # Tests the base functionality that should be identical across all cache stores.
5
+ module CacheStoreBehavior
6
+ def test_should_read_and_write_strings
7
+ @cache.write('foo', 'bar')
8
+ assert_equal 'bar', @cache.read('foo')
9
+ end
10
+
11
+ def test_should_read_and_write_hash
12
+ @cache.write('foo', {:a => "b"})
13
+ assert_equal({:a => "b"}, @cache.read('foo'))
14
+ end
15
+
16
+ def test_should_read_and_write_integer
17
+ @cache.write('foo', 1)
18
+ assert_equal 1, @cache.read('foo')
19
+ end
20
+
21
+ def test_should_read_and_write_nil
22
+ @cache.write('foo', nil)
23
+ assert_equal nil, @cache.read('foo')
24
+ end
25
+
26
+ def test_fetch_without_cache_miss
27
+ @cache.write('foo', 'bar')
28
+ assert_equal 'bar', @cache.fetch('foo') { 'baz' }
29
+ end
30
+
31
+ def test_fetch_with_cache_miss
32
+ assert_equal 'baz', @cache.fetch('foo') { 'baz' }
33
+ end
34
+
35
+ def test_fetch_with_forced_cache_miss
36
+ @cache.fetch('foo', :force => true) { 'bar' }
37
+ end
38
+
39
+ def test_increment
40
+ @cache.write('foo', 1, :raw => true)
41
+ assert_equal 1, @cache.read('foo', :raw => true).to_i
42
+ assert_equal 2, @cache.increment('foo')
43
+ assert_equal 2, @cache.read('foo', :raw => true).to_i
44
+ assert_equal 3, @cache.increment('foo')
45
+ assert_equal 3, @cache.read('foo', :raw => true).to_i
46
+ end
47
+
48
+ def test_decrement
49
+ @cache.write('foo', 3, :raw => true)
50
+ assert_equal 3, @cache.read('foo', :raw => true).to_i
51
+ assert_equal 2, @cache.decrement('foo')
52
+ assert_equal 2, @cache.read('foo', :raw => true).to_i
53
+ assert_equal 1, @cache.decrement('foo')
54
+ assert_equal 1, @cache.read('foo', :raw => true).to_i
55
+ end
56
+
57
+ def test_exist
58
+ @cache.write('foo', 'bar')
59
+ assert @cache.exist?('foo')
60
+ assert !@cache.exist?('bar')
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ require File.dirname(__FILE__) + '/cache_store_behavior.rb'
2
+ require 'test_helper'
3
+ require 'active_support/cache/localmemcache_store'
4
+
5
+ # Most tests borrowed from the Rails tests
6
+ # http://github.com/rails/rails/blob/master/activesupport/test/caching_test.rb
7
+
8
+ class LocalmemcacheStoreTest < ActiveSupport::TestCase
9
+
10
+ def setup
11
+ @cache = ActiveSupport::Cache.lookup_store(:localmemcache_store)
12
+ @cache.clear
13
+ @cache.silence!
14
+ @cache.logger = Logger.new("/dev/null")
15
+ end
16
+
17
+ test "write a value to the cache" do
18
+ assert_nothing_raised do
19
+ value = :value
20
+ ret = @cache.write :key, value
21
+ assert_equal ret, value
22
+ end
23
+ end
24
+
25
+ test "read a cached value" do
26
+ assert_nothing_raised do
27
+ value = :value
28
+ @cache.write :key, value
29
+ assert_equal value, @cache.read(:key)
30
+ end
31
+ end
32
+
33
+ test "entry expires" do
34
+ value = :value
35
+ @cache.write :key, value, :expires_in => 1.second
36
+ assert_equal :value, @cache.read(:key)
37
+ sleep 2
38
+ assert_nil @cache.read(:key)
39
+ end
40
+
41
+ include CacheStoreBehavior
42
+
43
+ def test_store_objects_should_be_immutable
44
+ @cache.write('foo', 'bar')
45
+ @cache.read('foo').gsub!(/.*/, 'baz')
46
+ assert_equal 'bar', @cache.read('foo')
47
+ end
48
+
49
+ def test_stored_objects_should_not_be_frozen
50
+ @cache.write('foo', 'bar')
51
+ assert !@cache.read('foo').frozen?
52
+ end
53
+
54
+ def test_write_should_return_true_on_success
55
+ result = @cache.write('foo', 'bar')
56
+ assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
57
+ assert result
58
+ end
59
+
60
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+
5
+ require File.dirname(__FILE__) + '/../rails/init.rb'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: localmemcache_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "Florian D\xC3\xBCtsch (der_flo)"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-11 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: mail@florian-duetsch.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib
26
+ - MIT-LICENSE
27
+ - pkg
28
+ - rails
29
+ - Rakefile
30
+ - README
31
+ - tasks
32
+ - test
33
+ - TODOs.txt
34
+ - lib/active_support
35
+ - lib/active_support/cache
36
+ - lib/active_support/cache/localmemcache_store.rb
37
+ - rails/init.rb
38
+ - tasks/localmemcache_store_tasks.rake
39
+ - test/cache_store_behavior.rb
40
+ - test/localmemcache_store_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: false
43
+ homepage: http://github.com/der-flo/localmemcache_store
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: A Rails cache store implementation for localmemcache
68
+ test_files: []
69
+