cached_singleton 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b4a68ba40df42d54333d5c44f6ea0c016b5070d
4
+ data.tar.gz: b8c47e7bfd9f418f340cb45ab409ea86b0af2866
5
+ SHA512:
6
+ metadata.gz: 89525792284ffe0dedf01631f2bef2f969fbba0ff7d3b91476953cf1ccb9f05e5f416f49cba5cc3e5265897abad8b36e2930fb943fd7efa24e03d4b5b061824f
7
+ data.tar.gz: b9875020570ada543cec2f03520b28097a844f49d0db29b398a24c5bc160ca6052cca830b9f57d9e4d2af9417ba56dc48736c2d98c1db54058c4154f8e510c59
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 telzamek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Cached::Singleton
2
+
3
+ It allows you to create a singleton from an active record
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cached-singleton'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cached-singleton
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,55 @@
1
+ require "cached_singleton/version"
2
+
3
+ module CachedSingleton
4
+ @@cache_strategy = nil
5
+
6
+ def self.default_cache_strategy=(cache_strategy)
7
+ @@cache_strategy = cache_strategy
8
+ end
9
+
10
+ def self.default_cache_strategy
11
+ unless @@cache_strategy
12
+ raise RuntimeError.new('default cache strategy not set')
13
+ end
14
+ @@cache_strategy
15
+ end
16
+
17
+ def self.included(model)
18
+ model.class_eval do
19
+
20
+ class << self
21
+ def cache
22
+ CachedSingleton.default_cache_strategy
23
+ end
24
+
25
+ def singleton_cache_key
26
+ "/#{self.name}"
27
+ end
28
+
29
+ def instance
30
+ # Fetch in cache
31
+ m = self.cache.read(singleton_cache_key)
32
+ return m if m
33
+
34
+ # Get it in from the DB or create it
35
+ transaction do
36
+ # We ensure no other process is trying to SELECT and INSERT the same record by locking the entire table (read and write) during the transaction
37
+ connection.execute("LOCK TABLE #{table_name} IN EXCLUSIVE MODE")
38
+ m = first || create
39
+ end
40
+
41
+ # Save it in cache if it's in the DB
42
+ self.cache.write(singleton_cache_key, m) if m.try(:persisted?)
43
+
44
+ m
45
+ end
46
+ end
47
+
48
+ after_commit :expire_cache
49
+ end
50
+ end
51
+
52
+ def expire_cache
53
+ self.class.cache.delete(self.class.singleton_cache_key) if (!new_record? || destroyed?)
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module CachedSingleton
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'cached_singleton/cached_singleton'
2
+ require 'cached_singleton/version'
@@ -0,0 +1,80 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'rack/test'
5
+ require 'minitest/autorun'
6
+ require 'cached_singleton'
7
+
8
+ class FakeModel
9
+ def self.after_commit(f)
10
+ end
11
+
12
+ def self.transaction
13
+ yield
14
+ end
15
+
16
+ def self.connection
17
+ FakeConnection.new
18
+ end
19
+
20
+ def self.create
21
+ end
22
+
23
+ def self.first
24
+ end
25
+
26
+ def try(s)
27
+ end
28
+
29
+ def self.table_name
30
+ 'table_name'
31
+ end
32
+
33
+ include CachedSingleton
34
+ end
35
+
36
+ class FakeConnection
37
+ def execute(r)
38
+ end
39
+ end
40
+
41
+ class FakeCache
42
+ class << self
43
+ def read(k)
44
+ end
45
+
46
+ def write(k)
47
+ end
48
+
49
+ def delete(k)
50
+ end
51
+ end
52
+ end
53
+
54
+ class CachedSingletonTest < MiniTest::Unit::TestCase
55
+ include Rack::Test::Methods
56
+
57
+ def test_singleton_cache_key
58
+ assert_equal FakeModel.singleton_cache_key, '/FakeModel'
59
+ end
60
+
61
+ def test_cache
62
+ CachedSingleton.default_cache_strategy = FakeCache
63
+ assert_equal FakeModel.cache, FakeCache
64
+ end
65
+
66
+ def test_single_instance_from_cache
67
+ CachedSingleton.default_cache_strategy = FakeCache
68
+ FakeCache.stub :read, FakeModel.new do
69
+ assert_same FakeModel.instance, FakeModel.instance
70
+ end
71
+ end
72
+
73
+ def test_single_instance_from_db
74
+ CachedSingleton.default_cache_strategy = FakeCache
75
+ FakeModel.stub :first, FakeModel.new do
76
+ assert_same FakeModel.instance, FakeModel.instance
77
+ end
78
+ end
79
+
80
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cached_singleton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thibault El Zamek, Cédric Darné, Lionel Oto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: It allows you to create a singleton from an active record
42
+ email:
43
+ - thibault.elzamek@c4mprod.com, cedric.darne@c4mprod.com, lionel.oto@c4mprod.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.txt
49
+ - README.md
50
+ - lib/cached_singleton/version.rb
51
+ - lib/cached_singleton/cached_singleton.rb
52
+ - lib/cached_singleton.rb
53
+ - spec/cached_singleton_spec.rb
54
+ homepage: https://github.com/c4mprod/cached_singleton
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.1.11
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: It allows you to create a singleton from an active record
78
+ test_files:
79
+ - spec/cached_singleton_spec.rb