tiny-cache 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +32 -0
  4. data/Rakefile +25 -0
  5. data/lib/tasks/tiny_cache_tasks.rake +4 -0
  6. data/lib/tiny-cache.rb +1 -0
  7. data/lib/tiny_cache.rb +17 -0
  8. data/lib/tiny_cache/active_record.rb +16 -0
  9. data/lib/tiny_cache/active_record/base.rb +147 -0
  10. data/lib/tiny_cache/active_record/belongs_to_association.rb +37 -0
  11. data/lib/tiny_cache/active_record/fetch_by_uniq_key.rb +54 -0
  12. data/lib/tiny_cache/active_record/finder_methods.rb +132 -0
  13. data/lib/tiny_cache/active_record/has_one_association.rb +36 -0
  14. data/lib/tiny_cache/active_record/persistence.rb +28 -0
  15. data/lib/tiny_cache/arel/wheres.rb +36 -0
  16. data/lib/tiny_cache/config.rb +23 -0
  17. data/lib/tiny_cache/record_marshal.rb +27 -0
  18. data/lib/tiny_cache/version.rb +4 -0
  19. data/spec/dummy/README.rdoc +28 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +3 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/spec/dummy/bin/bundle +3 -0
  27. data/spec/dummy/bin/rails +4 -0
  28. data/spec/dummy/bin/rake +4 -0
  29. data/spec/dummy/config.ru +4 -0
  30. data/spec/dummy/config/application.rb +24 -0
  31. data/spec/dummy/config/boot.rb +6 -0
  32. data/spec/dummy/config/database.yml +25 -0
  33. data/spec/dummy/config/environment.rb +6 -0
  34. data/spec/dummy/config/environments/development.rb +30 -0
  35. data/spec/dummy/config/environments/production.rb +81 -0
  36. data/spec/dummy/config/environments/test.rb +37 -0
  37. data/spec/dummy/config/initializers/backtrace_silencers.rb +8 -0
  38. data/spec/dummy/config/initializers/filter_parameter_logging.rb +5 -0
  39. data/spec/dummy/config/initializers/inflections.rb +17 -0
  40. data/spec/dummy/config/initializers/mime_types.rb +6 -0
  41. data/spec/dummy/config/initializers/secret_token.rb +13 -0
  42. data/spec/dummy/config/initializers/session_store.rb +4 -0
  43. data/spec/dummy/config/initializers/wrap_parameters.rb +15 -0
  44. data/spec/dummy/config/locales/en.yml +23 -0
  45. data/spec/dummy/config/routes.rb +57 -0
  46. data/spec/dummy/public/404.html +58 -0
  47. data/spec/dummy/public/422.html +58 -0
  48. data/spec/dummy/public/500.html +57 -0
  49. data/spec/dummy/public/favicon.ico +0 -0
  50. data/spec/spec_helper.rb +41 -0
  51. metadata +152 -0
@@ -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
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module TinyCache
3
+ VERSION = "0.0.3"
4
+ end
@@ -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>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,6 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class ApplicationController < ActionController::Base
3
+ # Prevent CSRF attacks by raising an exception.
4
+ # For APIs, you may want to use :null_session instead.
5
+ protect_from_forgery with: :exception
6
+ end
@@ -0,0 +1,3 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module ApplicationHelper
3
+ end
@@ -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>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -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__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000
@@ -0,0 +1,6 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # Load the Rails application.
3
+ require File.expand_path('../application', __FILE__)
4
+
5
+ # Initialize the Rails application.
6
+ Dummy::Application.initialize!
@@ -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