object_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in object_cache.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tom Caspy
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/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Tom Caspy
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.md ADDED
@@ -0,0 +1,43 @@
1
+ # ObjectCache
2
+
3
+ Simple object caching for ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'object_cache'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install object_cache
18
+
19
+ ## Usage
20
+
21
+ in an initializer define ObjectCache.source "path_to_yml_file"
22
+ and optional ObjectCache.ttl 5.minutes (or anything else)
23
+
24
+ From that moment on you can use ObjectCache.key to access the object you defined as "key".
25
+
26
+ The yml file looks like this:
27
+
28
+ key:
29
+ name: ClassName
30
+ id: 4
31
+ another_key:
32
+ name: ClassName
33
+ id: 5
34
+
35
+ it assumes ClassName has a method find which can be used by ClassName.find([4,5]) and returns the correct objects. (usually ActiveRecord, but can be used for mongoid objects, etc.)
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = object_cache
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to object_cache
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Tom Caspy. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,63 @@
1
+ require "object_reader"
2
+
3
+ class ObjectCache
4
+ @@cache ||= {}
5
+ @@ttl ||= 60
6
+ @@file_or_hash ||= nil
7
+ @@reader ||= nil
8
+ @@expires_at ||= nil
9
+
10
+
11
+ def self.source(file_or_hash)
12
+ @@file_or_hash = file_or_hash
13
+ end
14
+
15
+ def self.expire_after(ttl)
16
+ @@ttl = ttl
17
+ end
18
+
19
+ def self.refresh_the_cache
20
+ @@reader ||= Reader.new(@@file_or_hash)
21
+ @@cache ||= {}
22
+
23
+ groups = @@reader.grouped_objects
24
+ groups.each do |klass, ids|
25
+ #we need both ids and items to be in the same order for this to work
26
+ ids.sort!{|a,b| b.first <=> a.first}
27
+ items = constantize(klass).find(ids.collect(&:last))
28
+ items.sort!{|a,b| b.id <=> a.id}
29
+
30
+ items.each do |item|
31
+ i ||= 0
32
+ @@cache[ids[i][0].to_s] = item
33
+ i+=1
34
+ end
35
+
36
+ end
37
+ @@expires_at = Time.now + (@@ttl || 5.minutes)
38
+ end
39
+
40
+ def self.constantize(camel_cased_word)
41
+ names = camel_cased_word.split('::')
42
+ names.shift if names.empty? || names.first.empty?
43
+
44
+ constant = Object
45
+ names.each do |name|
46
+ constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
47
+ end
48
+ constant
49
+ end
50
+
51
+ def self.hash
52
+ @@hash
53
+ end
54
+
55
+ def self.method_missing(method_name)
56
+ if @@cache.empty? || @@expires_at < Time.now
57
+ refresh_the_cache
58
+ end
59
+
60
+ @@cache[method_name.to_s]
61
+ end
62
+
63
+ end
@@ -0,0 +1,3 @@
1
+ module ObjectCache
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'yaml'
2
+
3
+ class ObjectCache
4
+ class Reader
5
+ attr_accessor :hash
6
+
7
+ def initialize(source)
8
+
9
+ case source
10
+ when nil
11
+ raise Errno::ENOENT, "No file specified as ObjectCache source"
12
+ when Hash
13
+ self.hash = source
14
+ else
15
+ self.hash = YAML.load(ERB.new(open(source).read).result).to_hash
16
+ end
17
+
18
+ end
19
+
20
+ #creates a hashed key-value by the class names
21
+ def grouped_objects
22
+ product = {}
23
+ hash.each do |key,value|
24
+ product[value[:name]] ||= []
25
+ product[value[:name]] << [key, value[:id]]
26
+ end
27
+ product
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/object_cache/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tom Caspy"]
6
+ gem.email = ["tcaspy@gmail.com"]
7
+ gem.description = %q{simple object cache for ruby}
8
+ gem.summary = %q{hash table for object cache with defined TTL}
9
+ gem.homepage = "https://github.com/KensoDev/object_cache"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "object_cache"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ObjectCache::VERSION
17
+
18
+ gem.add_development_dependency(%q<rspec>, [">= 0"])
19
+ gem.add_development_dependency(%q<rake>, ["~> 0.9.2"])
20
+ end
@@ -0,0 +1,3 @@
1
+ foo:
2
+ name: "User"
3
+ id: 1
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe ObjectCache do
4
+
5
+ describe "reading the yml file" do
6
+ it "should read yml to a hash" do
7
+ o=ObjectCache::Reader.new(File.dirname(__FILE__)+"/fixtures/foo.yml")
8
+ o.hash.class.should == Hash
9
+ end
10
+ end
11
+
12
+ describe "caching" do
13
+ before :all do
14
+ @hash = {
15
+ foo: {
16
+ name: "User",
17
+ id: 1
18
+ },
19
+ bar: {
20
+ name: "User",
21
+ id: 2
22
+ },
23
+ baz: {
24
+ name: "Blah",
25
+ id: 4
26
+ }
27
+ }
28
+
29
+ ObjectCache.source @hash
30
+
31
+ class User
32
+ end
33
+
34
+ class Blah
35
+ end
36
+ end
37
+
38
+ it "should cache all of the objects in the hash" do
39
+ u1 = double(User)
40
+ u2 = double(User)
41
+ u1.stub!(:id).and_return(1)
42
+ u2.stub!(:id).and_return(2)
43
+ b = double(Blah)
44
+ b.stub!(:id).and_return(10)
45
+ User.should_receive(:find).with([1,2]).and_return([u1,u2])
46
+ Blah.should_receive(:find).with([4]).and_return([b])
47
+
48
+ ObjectCache.foo.should == u1
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,7 @@
1
+ require "object_cache"
2
+ RSpec.configure do |config|
3
+ # config.mock_with :mocha
4
+ # config.mock_with :flexmock
5
+ # config.mock_with :rr
6
+ config.mock_with :rspec
7
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Caspy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-17 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70360307204680 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70360307204680
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70360307884400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70360307884400
36
+ description: simple object cache for ruby
37
+ email:
38
+ - tcaspy@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - LICENSE.txt
47
+ - README.md
48
+ - README.rdoc
49
+ - Rakefile
50
+ - lib/object_cache.rb
51
+ - lib/object_cache/version.rb
52
+ - lib/object_reader.rb
53
+ - object_cache.gemspec
54
+ - spec/fixtures/foo.yml
55
+ - spec/object_cache_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/KensoDev/object_cache
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.10
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: hash table for object cache with defined TTL
81
+ test_files:
82
+ - spec/fixtures/foo.yml
83
+ - spec/object_cache_spec.rb
84
+ - spec/spec_helper.rb