restrict_cache 0.1.0

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: e4a37b2b0b86aaf69fda3297713e32fdd1951722
4
+ data.tar.gz: f221ff5b19e8c16e2844d0e45e70aa1989226ac8
5
+ SHA512:
6
+ metadata.gz: a80b5f4e4070bf06a1bd4ec645c1d05f320f5b69dce451779c524677168b5578dab87744061d127502185cad5171677f7a8775164f7ffec1a4df1053ba0a8cda
7
+ data.tar.gz: 2833d2da4631d6873c4e7b5fc8ba3a5f6809850d7f78a0e12e1432851a5752740e3c9969fb47ccb5f6328a43bbe4f3178b211488ff6d6928c416d3c7870adec7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in restrict_cache.gemspec
4
+ gemspec
5
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 i2bskn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # RestrictCache
2
+
3
+ Restricted cache in the rack request.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'restrict_cache'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install restrict_cache
20
+
21
+ ## Usage
22
+
23
+ Using the middleware or callbacks to clear the cache for each request.
24
+
25
+ #### Rack Middleware
26
+
27
+ ```ruby
28
+ module MyApp
29
+ class Application < Rails::Application
30
+ config.middleware.use RestrictCache::CacheSweeper
31
+ end
32
+ end
33
+ ```
34
+
35
+ #### Callbacks (Around Action)
36
+
37
+ ```ruby
38
+ class MyModel < ActiveRecord::Base; end
39
+ MyModel.find_with_restrict_cache(id_or_ids)
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/i2bskn/restrict_cache/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
55
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run all specs"
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ task :default => :spec
10
+
11
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "restrict_cache"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ module RestrictCache
2
+ module Accessible
3
+ THREAD_KEY = :restrict_cache
4
+
5
+ def collection
6
+ Thread.current[THREAD_KEY] ||= Cacheable.new
7
+ end
8
+
9
+ def clear
10
+ Thread.current[THREAD_KEY] = nil
11
+ end
12
+ end
13
+
14
+ extend Accessible
15
+ end
16
+
@@ -0,0 +1,21 @@
1
+ module RestrictCache
2
+ class ActionControllerExt
3
+ include ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def cache_sweep_action(*args)
7
+ options = args.extract_options!
8
+ actions = args.empty? ? {} : {only: args}
9
+ options.merge! actions unless actions.empty?
10
+ around_action :sweep_restrict_cache, options
11
+ end
12
+ end
13
+
14
+ def sweep_restrict_cache
15
+ yield
16
+ ensure
17
+ RestrictCache.clear
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,53 @@
1
+ module RestrictCache
2
+ module ActiveRecordExt
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def find_and_restrict_cache(arg)
9
+ records = find(arg)
10
+ Array(records).each(&:restrict_cache)
11
+ records
12
+ end
13
+
14
+ def find_from_restrict_cache(arg)
15
+ contents = RestrictCache.collection.active_record.contents(self.table_name)
16
+ return nil unless contents
17
+
18
+ case arg
19
+ when Integer
20
+ contents[arg]
21
+ when String
22
+ contents[arg.to_i]
23
+ when Array
24
+ args.map {|index| contents[index.to_i] }
25
+ else
26
+ raise "unknown argument: #{arg.inspect}"
27
+ end
28
+ end
29
+
30
+ def find_with_restrict_cache(arg)
31
+ if restrict_cached?(Array(arg))
32
+ records = Array(arg).map {|index| find_from_restrict_cache(index) }
33
+ records.size > 1 ? records : records.first
34
+ else
35
+ find_and_restrict_cache(arg)
36
+ end
37
+ end
38
+
39
+ private
40
+ def restrict_cached?(args)
41
+ content = RestrictCache.collection.active_record.contents(self.table_name)
42
+ return false unless content
43
+ ids = content.keys
44
+ args.all? {|index| ids.include?(index) }
45
+ end
46
+ end
47
+
48
+ def restrict_cache
49
+ RestrictCache.collection.add(self)
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,14 @@
1
+ module RestrictCache
2
+ class CacheSweeper
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ @app.call(env)
9
+ ensure
10
+ RestrictCache.clear
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,20 @@
1
+ module RestrictCache
2
+ class Cacheable
3
+ class ActiveRecordCache < Base
4
+ def add(content)
5
+ @caches[table_name(content)] ||= {}
6
+ @caches[table_name(content)][content.id] = content
7
+ end
8
+
9
+ def contents(_table_name)
10
+ @caches[_table_name.to_sym]
11
+ end
12
+
13
+ private
14
+ def table_name(content)
15
+ content.class.table_name.to_sym
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,22 @@
1
+ module RestrictCache
2
+ class Cacheable
3
+ class Base
4
+ def initialize
5
+ @caches = {}
6
+ end
7
+
8
+ def add(content)
9
+ raise NotImplementedError
10
+ end
11
+
12
+ def contents(_table_name)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def size
17
+ @caches.size
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,13 @@
1
+ module RestrictCache
2
+ class Cacheable
3
+ class CustomCache < Base
4
+ def add(cache_key, content)
5
+ @caches[cache_key.to_sym] = content
6
+ end
7
+
8
+ def contents(cache_key = nil)
9
+ cache_key ? @caches[cache_key.to_sym] : nil
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ require "restrict_cache/cacheable/base"
2
+ require "restrict_cache/cacheable/active_record_cache"
3
+ require "restrict_cache/cacheable/custom_cache"
4
+
5
+ module RestrictCache
6
+ class Cacheable
7
+ module CacheKey
8
+ ACTIVERECORD = :active_record_cache
9
+ CUSTOM = :custom_cache
10
+ ALL = [ACTIVERECORD, CUSTOM]
11
+
12
+ def self.get(content)
13
+ if defined?(ActiveRecord) && content.class < ActiveRecord::Base
14
+ return ACTIVERECORD
15
+ end
16
+ CUSTOM
17
+ end
18
+ end
19
+
20
+ class << self
21
+ def build_inner_cache(key)
22
+ const_get(key.to_s.classify, false).new
23
+ end
24
+ end
25
+
26
+ CacheKey::ALL.each do |inner_cache_key|
27
+ define_method inner_cache_key do
28
+ inner_cache inner_cache_key
29
+ end
30
+ end
31
+
32
+ def initialize
33
+ @cache_collection = {}
34
+ end
35
+
36
+ def add(content)
37
+ inner_cache(CacheKey.get(content)).add(content)
38
+ end
39
+
40
+ def [](key)
41
+ return nil unless key
42
+ @cache_collection[key]
43
+ end
44
+
45
+ def cache_keys
46
+ CacheKey::ALL
47
+ end
48
+
49
+ private
50
+ def inner_cache(key)
51
+ @cache_collection[key] ||= self.class.build_inner_cache(key)
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,17 @@
1
+ module RestrictCache
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "restrict_cache" do
4
+ require "restrict_cache/action_controller_ext"
5
+ require "restrict_cache/active_record_ext"
6
+
7
+ ActiveSupport.on_load(:action_controller) do
8
+ ::ActionController::Base.send(:include, RestrictCache::ActionControllerExt)
9
+ end
10
+
11
+ ActiveSupport.on_load(:active_record) do
12
+ ::ActiveRecord::Base.send(:include, RestrictCache::ActiveRecordExt)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,3 @@
1
+ module RestrictCache
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "restrict_cache/version"
2
+ require "restrict_cache/accessible"
3
+ require "restrict_cache/cacheable"
4
+ require "restrict_cache/cache_sweeper"
5
+ require "restrict_cache/railtie" if defined?(Rails)
6
+
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'restrict_cache/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "restrict_cache"
7
+ spec.version = RestrictCache::VERSION
8
+ spec.authors = ["i2bskn"]
9
+ spec.email = ["i2bskn@gmail.com"]
10
+
11
+ spec.summary = %q{Restricted cache in the rack request.}
12
+ spec.description = %q{Restricted cache in the rack request.}
13
+ spec.homepage = "https://github.com/i2bskn/restrict_cache"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restrict_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-14 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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
+ description: Restricted cache in the rack request.
56
+ email:
57
+ - i2bskn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/restrict_cache.rb
73
+ - lib/restrict_cache/accessible.rb
74
+ - lib/restrict_cache/action_controller_ext.rb
75
+ - lib/restrict_cache/active_record_ext.rb
76
+ - lib/restrict_cache/cache_sweeper.rb
77
+ - lib/restrict_cache/cacheable.rb
78
+ - lib/restrict_cache/cacheable/active_record_cache.rb
79
+ - lib/restrict_cache/cacheable/base.rb
80
+ - lib/restrict_cache/cacheable/custom_cache.rb
81
+ - lib/restrict_cache/railtie.rb
82
+ - lib/restrict_cache/version.rb
83
+ - restrict_cache.gemspec
84
+ homepage: https://github.com/i2bskn/restrict_cache
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.4.5
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Restricted cache in the rack request.
108
+ test_files: []