tiny_cache 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +1 -1
- data/README.md +32 -0
- data/Rakefile +15 -30
- data/lib/tiny_cache.rb +14 -1
- data/lib/tiny_cache/active_record.rb +18 -0
- data/lib/tiny_cache/active_record/base.rb +133 -0
- data/lib/tiny_cache/active_record/belongs_to_association.rb +37 -0
- data/lib/tiny_cache/active_record/fetch_by_uniq_key.rb +39 -0
- data/lib/tiny_cache/active_record/finder_methods.rb +132 -0
- data/lib/tiny_cache/active_record/has_many_association.rb +37 -0
- data/lib/tiny_cache/active_record/has_one_association.rb +36 -0
- data/lib/tiny_cache/active_record/persistence.rb +28 -0
- data/lib/tiny_cache/arel/wheres.rb +36 -0
- data/lib/tiny_cache/config.rb +23 -0
- data/lib/tiny_cache/record_marshal.rb +27 -0
- data/lib/tiny_cache/version.rb +2 -1
- data/spec/dummy/README.rdoc +28 -0
- data/{test → spec}/dummy/Rakefile +0 -1
- data/{test → spec}/dummy/app/assets/javascripts/application.js +3 -5
- data/{app/assets/stylesheets/tiny_cache → spec/dummy/app/assets/stylesheets}/application.css +0 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -0
- data/{test → spec}/dummy/app/helpers/application_helper.rb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/{test → spec}/dummy/config.ru +1 -1
- data/spec/dummy/config/application.rb +24 -0
- data/spec/dummy/config/boot.rb +6 -0
- data/{test → spec}/dummy/config/database.yml +0 -0
- data/spec/dummy/config/environment.rb +6 -0
- data/spec/dummy/config/environments/development.rb +30 -0
- data/spec/dummy/config/environments/production.rb +81 -0
- data/{test → spec}/dummy/config/environments/test.rb +14 -14
- data/{test → spec}/dummy/config/initializers/backtrace_silencers.rb +1 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +5 -0
- data/spec/dummy/config/initializers/inflections.rb +17 -0
- data/{test → spec}/dummy/config/initializers/mime_types.rb +1 -0
- data/spec/dummy/config/initializers/secret_token.rb +13 -0
- data/spec/dummy/config/initializers/session_store.rb +4 -0
- data/{test → spec}/dummy/config/initializers/wrap_parameters.rb +7 -6
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +57 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/{test → spec}/dummy/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +41 -0
- metadata +95 -84
- data/README.rdoc +0 -3
- data/app/assets/javascripts/tiny_cache/application.js +0 -15
- data/app/controllers/tiny_cache/application_controller.rb +0 -4
- data/app/helpers/tiny_cache/application_helper.rb +0 -4
- data/app/views/layouts/tiny_cache/application.html.erb +0 -14
- data/config/routes.rb +0 -2
- data/lib/tiny_cache/engine.rb +0 -5
- data/test/dummy/README.rdoc +0 -261
- data/test/dummy/app/assets/stylesheets/application.css +0 -13
- data/test/dummy/app/controllers/application_controller.rb +0 -3
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/config/application.rb +0 -59
- data/test/dummy/config/boot.rb +0 -10
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/environments/development.rb +0 -37
- data/test/dummy/config/environments/production.rb +0 -67
- data/test/dummy/config/initializers/inflections.rb +0 -15
- data/test/dummy/config/initializers/secret_token.rb +0 -7
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/locales/en.yml +0 -5
- data/test/dummy/config/routes.rb +0 -4
- data/test/dummy/public/404.html +0 -26
- data/test/dummy/public/422.html +0 -26
- data/test/dummy/public/500.html +0 -25
- data/test/dummy/script/rails +0 -6
- data/test/integration/navigation_test.rb +0 -10
- data/test/test_helper.rb +0 -15
- data/test/tiny_cache_test.rb +0 -7
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyCache
|
3
|
+
module ActiveRecord
|
4
|
+
module Associations
|
5
|
+
module HasManyAssociation
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
|
8
|
+
# FIXME 尚未实现!!
|
9
|
+
|
10
|
+
included do
|
11
|
+
class_eval do
|
12
|
+
alias_method_chain :find_target, :tiny_cache
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def association_class
|
17
|
+
@reflection.klass
|
18
|
+
end
|
19
|
+
|
20
|
+
# 在缓存中取的数据
|
21
|
+
def find_target_with_tiny_cache
|
22
|
+
return find_target_without_tiny_cache unless association_class.tiny_cache_enabled?
|
23
|
+
records =
|
24
|
+
if options[:finder_sql]
|
25
|
+
reflection.klass.find_by_sql(custom_finder_sql)
|
26
|
+
else
|
27
|
+
scoped.all
|
28
|
+
end
|
29
|
+
|
30
|
+
records = options[:uniq] ? uniq(records) : records
|
31
|
+
records.each { |record| set_inverse_instance(record) }
|
32
|
+
records
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyCache
|
3
|
+
module ActiveRecord
|
4
|
+
module Associations
|
5
|
+
module HasOneAssociation
|
6
|
+
extend ::ActiveSupport::Concern
|
7
|
+
included do
|
8
|
+
class_eval do
|
9
|
+
alias_method_chain :find_target, :tiny_cache
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def association_class
|
14
|
+
@reflection.klass
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_target_with_tiny_cache
|
18
|
+
return find_target_without_tiny_cache unless association_class.tiny_cache_enabled?
|
19
|
+
|
20
|
+
cache_record = association_class.tiny_cache_find_by(
|
21
|
+
reflection.foreign_key => owner[reflection.active_record_primary_key]
|
22
|
+
)
|
23
|
+
|
24
|
+
return cache_record.tap{|record| set_inverse_instance(record)} if cache_record
|
25
|
+
|
26
|
+
record = find_target_without_tiny_cache
|
27
|
+
|
28
|
+
record.tap do |r|
|
29
|
+
set_inverse_instance(r)
|
30
|
+
r.write_tiny_cache
|
31
|
+
end if record
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyCache
|
3
|
+
module ActiveRecord
|
4
|
+
module Persistence
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
class_eval do
|
9
|
+
alias_method_chain :reload, :tiny_cache
|
10
|
+
alias_method_chain :touch, :tiny_cache
|
11
|
+
alias_method_chain :update_column, :tiny_cache
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def update_column_with_tiny_cache(name, value)
|
16
|
+
update_column_without_tiny_cache(name, value).tap{update_tiny_cache}
|
17
|
+
end
|
18
|
+
|
19
|
+
def reload_with_tiny_cache(options = nil)
|
20
|
+
reload_without_tiny_cache(options).tap{expire_tiny_cache}
|
21
|
+
end
|
22
|
+
|
23
|
+
def touch_with_tiny_cache(name = nil)
|
24
|
+
touch_without_tiny_cache(name).tap{update_tiny_cache}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyCache
|
3
|
+
module Arel
|
4
|
+
class Wheres
|
5
|
+
attr_reader :where_values
|
6
|
+
|
7
|
+
def initialize(where_values)
|
8
|
+
@where_values = where_values
|
9
|
+
end
|
10
|
+
|
11
|
+
# Determine whether all conditions is equality, for example:
|
12
|
+
#
|
13
|
+
# Article.where("user_id = 1").where(:status => 1).find(1)
|
14
|
+
def all_equality?
|
15
|
+
where_values.all?{|where_value| where_value.is_a?(::Arel::Nodes::Equality)}
|
16
|
+
end
|
17
|
+
|
18
|
+
# Extract conditions to pairs, for checking whether cache match the conditions.
|
19
|
+
def extract_pairs
|
20
|
+
where_values.map do |where_value|
|
21
|
+
if where_value.is_a?(String)
|
22
|
+
left, right = where_value.split(/\s*=\s*/, 2)
|
23
|
+
right = right.to_i
|
24
|
+
else
|
25
|
+
left, right = where_value.left.name, where_value.right
|
26
|
+
end
|
27
|
+
|
28
|
+
{
|
29
|
+
:left => left,
|
30
|
+
:right => right
|
31
|
+
}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinyCache
|
3
|
+
module Config
|
4
|
+
extend self
|
5
|
+
|
6
|
+
attr_accessor :cache_store, :logger, :cache_key_prefix
|
7
|
+
|
8
|
+
def cache_store
|
9
|
+
@cache_store ||= ::Rails.cache if defined?(::Rails)
|
10
|
+
@cache_store
|
11
|
+
end
|
12
|
+
|
13
|
+
def logger
|
14
|
+
@logger ||= ::Rails.logger if defined?(::Rails)
|
15
|
+
@logger ||= ::Logger.new(STDOUT)
|
16
|
+
end
|
17
|
+
|
18
|
+
def cache_key_prefix
|
19
|
+
@cache_key_prefix ||= 'tiny_cache'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module RecordMarshal
|
3
|
+
class << self
|
4
|
+
# dump ActiveRecord instace with only attributes.
|
5
|
+
# ["User",
|
6
|
+
# {"id"=>30,
|
7
|
+
# "email"=>"dddssddd@gmail.com",
|
8
|
+
# "created_at"=>2012-07-25 18:25:57 UTC
|
9
|
+
# }
|
10
|
+
# ]
|
11
|
+
|
12
|
+
def dump(record)
|
13
|
+
[
|
14
|
+
record.class.name,
|
15
|
+
record.instance_variable_get(:@attributes)
|
16
|
+
]
|
17
|
+
end
|
18
|
+
|
19
|
+
# load a cached record
|
20
|
+
def load(serialized)
|
21
|
+
return unless serialized
|
22
|
+
record = serialized[0].constantize.allocate
|
23
|
+
record.init_with('attributes' => serialized[1])
|
24
|
+
record
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/tiny_cache/version.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
@@ -5,11 +5,9 @@
|
|
5
5
|
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
6
|
//
|
7
7
|
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
-
//
|
8
|
+
// compiled file.
|
9
9
|
//
|
10
|
-
//
|
11
|
-
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
12
|
//
|
13
|
-
//= require jquery
|
14
|
-
//= require jquery_ujs
|
15
13
|
//= require_tree .
|
data/{app/assets/stylesheets/tiny_cache → spec/dummy/app/assets/stylesheets}/application.css
RENAMED
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
|
6
|
+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require File.expand_path('../boot', __FILE__)
|
3
|
+
|
4
|
+
require 'rails/all'
|
5
|
+
|
6
|
+
Bundler.require(*Rails.groups)
|
7
|
+
require "tiny_cache"
|
8
|
+
|
9
|
+
module Dummy
|
10
|
+
class Application < Rails::Application
|
11
|
+
# Settings in config/environments/* take precedence over those specified here.
|
12
|
+
# Application configuration should go into files in config/initializers
|
13
|
+
# -- all .rb files in that directory are automatically loaded.
|
14
|
+
|
15
|
+
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
|
16
|
+
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
|
17
|
+
# config.time_zone = 'Central Time (US & Canada)'
|
18
|
+
|
19
|
+
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
|
20
|
+
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
|
21
|
+
# config.i18n.default_locale = :de
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
# Set up gems listed in the Gemfile.
|
3
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
|
4
|
+
|
5
|
+
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
|
6
|
+
$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
|
File without changes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
Dummy::Application.configure do
|
3
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
4
|
+
|
5
|
+
# In the development environment your application's code is reloaded on
|
6
|
+
# every request. This slows down response time but is perfect for development
|
7
|
+
# since you don't have to restart the web server when you make code changes.
|
8
|
+
config.cache_classes = false
|
9
|
+
|
10
|
+
# Do not eager load code on boot.
|
11
|
+
config.eager_load = false
|
12
|
+
|
13
|
+
# Show full error reports and disable caching.
|
14
|
+
config.consider_all_requests_local = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Don't care if the mailer can't send.
|
18
|
+
config.action_mailer.raise_delivery_errors = false
|
19
|
+
|
20
|
+
# Print deprecation notices to the Rails logger.
|
21
|
+
config.active_support.deprecation = :log
|
22
|
+
|
23
|
+
# Raise an error on page load if there are pending migrations
|
24
|
+
config.active_record.migration_error = :page_load
|
25
|
+
|
26
|
+
# Debug mode disables concatenation and preprocessing of assets.
|
27
|
+
# This option may cause significant delays in view rendering with a large
|
28
|
+
# number of complex assets.
|
29
|
+
config.assets.debug = true
|
30
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
Dummy::Application.configure do
|
3
|
+
# Settings specified here will take precedence over those in config/application.rb.
|
4
|
+
|
5
|
+
# Code is not reloaded between requests.
|
6
|
+
config.cache_classes = true
|
7
|
+
|
8
|
+
# Eager load code on boot. This eager loads most of Rails and
|
9
|
+
# your application in memory, allowing both thread web servers
|
10
|
+
# and those relying on copy on write to perform better.
|
11
|
+
# Rake tasks automatically ignore this option for performance.
|
12
|
+
config.eager_load = true
|
13
|
+
|
14
|
+
# Full error reports are disabled and caching is turned on.
|
15
|
+
config.consider_all_requests_local = false
|
16
|
+
config.action_controller.perform_caching = true
|
17
|
+
|
18
|
+
# Enable Rack::Cache to put a simple HTTP cache in front of your application
|
19
|
+
# Add `rack-cache` to your Gemfile before enabling this.
|
20
|
+
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
|
21
|
+
# config.action_dispatch.rack_cache = true
|
22
|
+
|
23
|
+
# Disable Rails's static asset server (Apache or nginx will already do this).
|
24
|
+
config.serve_static_assets = false
|
25
|
+
|
26
|
+
# Compress JavaScripts and CSS.
|
27
|
+
config.assets.js_compressor = :uglifier
|
28
|
+
# config.assets.css_compressor = :sass
|
29
|
+
|
30
|
+
# Do not fallback to assets pipeline if a precompiled asset is missed.
|
31
|
+
config.assets.compile = false
|
32
|
+
|
33
|
+
# Generate digests for assets URLs.
|
34
|
+
config.assets.digest = true
|
35
|
+
|
36
|
+
# Version of your assets, change this if you want to expire all your assets.
|
37
|
+
config.assets.version = '1.0'
|
38
|
+
|
39
|
+
# Specifies the header that your server uses for sending files.
|
40
|
+
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
|
41
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
|
42
|
+
|
43
|
+
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
44
|
+
# config.force_ssl = true
|
45
|
+
|
46
|
+
# Set to :debug to see everything in the log.
|
47
|
+
config.log_level = :info
|
48
|
+
|
49
|
+
# Prepend all log lines with the following tags.
|
50
|
+
# config.log_tags = [ :subdomain, :uuid ]
|
51
|
+
|
52
|
+
# Use a different logger for distributed setups.
|
53
|
+
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
|
54
|
+
|
55
|
+
# Use a different cache store in production.
|
56
|
+
# config.cache_store = :mem_cache_store
|
57
|
+
|
58
|
+
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
|
59
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
60
|
+
|
61
|
+
# Precompile additional assets.
|
62
|
+
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
|
63
|
+
# config.assets.precompile += %w( search.js )
|
64
|
+
|
65
|
+
# Ignore bad email addresses and do not raise email delivery errors.
|
66
|
+
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
|
67
|
+
# config.action_mailer.raise_delivery_errors = false
|
68
|
+
|
69
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
70
|
+
# the I18n.default_locale when a translation can not be found).
|
71
|
+
config.i18n.fallbacks = true
|
72
|
+
|
73
|
+
# Send deprecation notices to registered listeners.
|
74
|
+
config.active_support.deprecation = :notify
|
75
|
+
|
76
|
+
# Disable automatic flushing of the log to improve performance.
|
77
|
+
# config.autoflush_log = false
|
78
|
+
|
79
|
+
# Use default logging formatter so that PID and timestamp are not suppressed.
|
80
|
+
config.log_formatter = ::Logger::Formatter.new
|
81
|
+
end
|