cache_finder 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: c2683c37cf48d2d6efc2fba15a6ac2a4629c2477
4
+ data.tar.gz: f6649e98e3b25b144c92a9a892224e07bf9a5322
5
+ SHA512:
6
+ metadata.gz: 75f33e9aa498771f77d4566db54226d777e81f30425deaf1beb22dcebb38855ae6ac6bd2f734559e8c6e21a74b0a63385648e84832ac72d47ef03c7d84466db3
7
+ data.tar.gz: ed97007fea95d75f5b2b09593f08918c26127388adb410f8bc8f3a350ea73e669858f74ce86e81f3f7f5bd6f584f4223ef5bdd19e942701f4d771f372265b339
data/.gitignore ADDED
@@ -0,0 +1,21 @@
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
18
+
19
+ .DS_Store
20
+ .idea/
21
+ db/*.db
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cache_finder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alex
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
+ # CacheFinder
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cache_finder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cache_finder
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
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cache_finder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cache_finder'
8
+ spec.version = CacheFinder::VERSION
9
+ spec.authors = ['Shilov Alexander']
10
+ spec.email = ['sashapashamasha@gmail.com']
11
+ spec.description = 'A little cache managing system for Rails'
12
+ spec.summary = ''
13
+ spec.homepage = 'https://github.com/shlima/CacheFinder'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'activerecord'
26
+ spec.add_development_dependency 'sqlite3'
27
+
28
+ spec.add_dependency 'activesupport', '>= 3.2', '< 5.0'
29
+ spec.add_dependency 'actionpack', '>= 3.2', '< 5.0'
30
+ end
data/db/.keep ADDED
File without changes
@@ -0,0 +1,27 @@
1
+ require 'cache_finder/version'
2
+
3
+ # Custom
4
+ require 'cache_finder/finder/find_by_id'
5
+ require 'cache_finder/finder/container'
6
+ require 'cache_finder/cleaner/clean_by_id'
7
+ require 'cache_finder/cleaner/cleaner'
8
+
9
+ module CacheFinder
10
+
11
+ module Finder
12
+ module Singleton
13
+ include CacheFinder::FindById
14
+ include CacheFinder::Container
15
+ end
16
+
17
+ def self.included(base)
18
+ base.extend Singleton
19
+ end
20
+ end
21
+
22
+ module Cleaner
23
+ extend ActiveSupport::Concern
24
+ include CacheCleaner::CleanById
25
+ include CacheCleaner::Cleaner
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ module CacheCleaner
4
+
5
+ # Adding the after_save callback to the model.
6
+ # After saving the method will automatically drop cache
7
+ # by key: ['Class_name', id]
8
+
9
+ module CleanById
10
+ extend ActiveSupport::Concern
11
+ included do
12
+ after_save { drop_cache_by_id }
13
+ end
14
+
15
+ def drop_cache_by_id
16
+ Rails.cache.delete([self.class.name, self.id])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ module CacheCleaner
4
+
5
+ module Cleaner
6
+ include CacheFinder::Helper
7
+
8
+ def cache_cleaner(*keys)
9
+ keys.each do |key|
10
+ Rails.cache.delete(cache_container_key?(key))
11
+ end
12
+ end
13
+
14
+ # CacheFinder::Cleaner.cache
15
+ def cache(*keys)
16
+ keys.each do |key|
17
+ Rails.cache.delete(key)
18
+ end
19
+ end
20
+
21
+ module_function :cache
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ module CacheFinder
4
+
5
+ module Helper
6
+ # Helper method.
7
+ # You could find out the way of generating keys by calling
8
+ # this method on a class.
9
+ #
10
+ # Example:
11
+ # Chain.cache_container_key?(:name) # => ["Chain", :name]
12
+ #
13
+ def cache_container_key?(data)
14
+ key = *data
15
+ key.unshift(self.name)
16
+ key
17
+ end
18
+ end
19
+
20
+ # Will cached everything placed in a block.
21
+ #
22
+ # Example of using:
23
+ # Company.cache_container(key: :companies) { Company.all }
24
+ #
25
+ # Included in a module, the *cache_container* method will automatically
26
+ # add a *Class_name* to the key of the cache. In this way the key will be:
27
+ # ['Class_name', key]
28
+ #
29
+ # You can use container standalone, example of using:
30
+ # CacheFinder::Container.cache(key: [:company, :name]) { 'Microsoft' }
31
+ #
32
+ module Container
33
+ include CacheFinder::Helper
34
+
35
+ def cache_container(options, &block)
36
+ key = cache_container_key?(options[:key])
37
+ Rails.cache.fetch(key) { block.call }
38
+ end
39
+
40
+ # CacheFinder::Container.cache
41
+ def cache(options, &block)
42
+ key = options[:key]
43
+ Rails.cache.fetch(key) { block.call }
44
+ end
45
+ module_function :cache
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ module CacheFinder
4
+
5
+ # Will find and cached the entity by it's id.
6
+ #
7
+ # Example of using:
8
+ # Company.cache_find(10)
9
+ #
10
+ # Will produce the cache accessible by the key:
11
+ # ['Company', 10]
12
+
13
+ module FindById
14
+ def cache_find(id)
15
+ Rails.cache.fetch([self.name, id]) { find(id) }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module CacheFinder
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,26 @@
1
+ #require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe 'CacheFinder' do
4
+ before(:all) do
5
+ @company = Company.create(name: 'Brothers & Co.', phone: '315297032596', street: 'Time Square')
6
+ end
7
+
8
+ it 'works' do
9
+ @company.name.should == Company.find(@company.id).name
10
+ end
11
+
12
+ it 'cache_container_key?' do
13
+ Company.cache_container_key?(:name).should == ['Company', :name]
14
+ end
15
+
16
+ it 'cache_finder works' do
17
+ all = Company.cache_container(key: [:companies, :all]) { Company.all }
18
+ Rails.cache.read([Company.name, :companies, :all]).should == all
19
+ end
20
+
21
+ it 'cache_cleaner works' do
22
+ @company.name = 'Warner'
23
+ @company.save
24
+ Rails.cache.read([:companies, :all]).should == nil
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Company do
4
+
5
+ before(:all) do
6
+ @company = Company.create(name: 'Brothers & Co.', phone: '315297032596', street: 'Time Square')
7
+ end
8
+
9
+ it 'works' do
10
+ @company.name.should == Company.find(@company.id).name
11
+ end
12
+
13
+ it 'cache_find works' do
14
+ @company.name.should == Company.cache_find(@company.id).name
15
+ end
16
+
17
+ it 'caching works' do
18
+ Rails.cache.read([Company.name, @company.id]).should == @company
19
+ end
20
+
21
+ it 'caching-clear works' do
22
+ @company.name = 'Shliman'
23
+ @company.save
24
+ Rails.cache.read([Company.name, @company.id]).should == nil
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Company do
4
+ before(:all) do
5
+ @company = Company.create(name: 'Brothers & Co.', phone: '315297032596', street: 'Time Square')
6
+ end
7
+
8
+ it 'Clearing cache by string key' do
9
+ key = :cache
10
+ msg = 'Hola, Viola!'
11
+ CacheFinder::Container.cache(key: key) { msg }
12
+ Rails.cache.read(key).should == msg
13
+ CacheCleaner::Cleaner.cache(key)
14
+ Rails.cache.read(key).should == nil
15
+ end
16
+ end
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'active_support'
5
+ require 'active_record'
6
+ require 'action_controller'
7
+ require 'action_view'
8
+
9
+ require 'cache_finder'
10
+
11
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
12
+ ActiveRecord::Base.establish_connection(
13
+ adapter: 'sqlite3',
14
+ database: "#{root}/db/cache_finder_test.db"
15
+ )
16
+
17
+ # stub the Rails module functionality
18
+ RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_store)
19
+ module Rails
20
+ def self.cache
21
+ RAILS_CACHE
22
+ end
23
+ end
24
+
25
+ # Fake DB
26
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'companies'")
27
+ ActiveRecord::Base.connection.create_table(:companies) do |t|
28
+ t.string :name
29
+ t.string :phone
30
+ t.string :street
31
+ end
32
+
33
+ class Company < ActiveRecord::Base
34
+ include CacheFinder::Finder, CacheFinder::Cleaner
35
+ after_save { cache_cleaner [:companies, :all] }
36
+ end
37
+
38
+ # This file was generated by the `rspec --init` command. Conventionally, all
39
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
40
+ # Require this file using `require "spec_helper"` to ensure that it is only
41
+ # loaded once.
42
+ #
43
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
44
+ RSpec.configure do |config|
45
+ config.treat_symbols_as_metadata_keys_with_true_values = true
46
+ config.run_all_when_everything_filtered = true
47
+ config.filter_run :focus
48
+
49
+ # Run specs in random order to surface order dependencies. If you find an
50
+ # order dependency and want to debug it, you can fix the order by providing
51
+ # the seed, which is printed after each run.
52
+ # --seed 1234
53
+ config.order = 'random'
54
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cache_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shilov Alexander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-08 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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ - - <
91
+ - !ruby/object:Gem::Version
92
+ version: '5.0'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '3.2'
100
+ - - <
101
+ - !ruby/object:Gem::Version
102
+ version: '5.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: actionpack
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '3.2'
110
+ - - <
111
+ - !ruby/object:Gem::Version
112
+ version: '5.0'
113
+ type: :runtime
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '3.2'
120
+ - - <
121
+ - !ruby/object:Gem::Version
122
+ version: '5.0'
123
+ description: A little cache managing system for Rails
124
+ email:
125
+ - sashapashamasha@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - .gitignore
131
+ - .rspec
132
+ - Gemfile
133
+ - LICENSE.txt
134
+ - README.md
135
+ - Rakefile
136
+ - cache_finder.gemspec
137
+ - db/.keep
138
+ - lib/cache_finder.rb
139
+ - lib/cache_finder/cleaner/clean_by_id.rb
140
+ - lib/cache_finder/cleaner/cleaner.rb
141
+ - lib/cache_finder/finder/container.rb
142
+ - lib/cache_finder/finder/find_by_id.rb
143
+ - lib/cache_finder/version.rb
144
+ - spec/find/cache_finder_spec.rb
145
+ - spec/find/find_and_clear_by_id_spec.rb
146
+ - spec/find/stadalone_spec.rb
147
+ - spec/spec_helper.rb
148
+ homepage: https://github.com/shlima/CacheFinder
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.0.3
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: ''
172
+ test_files:
173
+ - spec/find/cache_finder_spec.rb
174
+ - spec/find/find_and_clear_by_id_spec.rb
175
+ - spec/find/stadalone_spec.rb
176
+ - spec/spec_helper.rb