Permalink
Browse files

init

  • Loading branch information...
0 parents commit 042d3fab53e13cbff2dd20f2890ec6d7702c9dcf @mustafaturan committed Jun 12, 2013
Showing with 593 additions and 0 deletions.
  1. +34 −0 CHANGE-LOG
  2. +17 −0 Gemfile
  3. +99 −0 Gemfile.lock
  4. +22 −0 LICENSE
  5. +22 −0 MIT-LICENSE
  6. +108 −0 README.rdoc
  7. +38 −0 Rakefile
  8. +28 −0 find_cache.gemspec
  9. +6 −0 lib/find_cache.rb
  10. +145 −0 lib/find_cache/cacheable.rb
  11. +3 −0 lib/find_cache/init.rb
  12. +63 −0 lib/find_cache/key_gen.rb
  13. +4 −0 lib/find_cache/version.rb
  14. +4 −0 lib/tasks/find_cache_tasks.rake
@@ -0,0 +1,34 @@
+version 0.2.1
+# fixed expire_find_cache bug (which occured in v0.2.0)
+
+version 0.2.0
+# find_by_id and find_all_by_id methods converted to where queries for rails 4
+# id based queries replaced with primary_key based queries
+
+version 0.1.8
+# local memorization on dynamic methods removed to prevent duplicate caching
+
+version 0.1.7
+# code cleanup and documentation
+
+version 0.1.6
+# after_commit on: :create, :update, :save instead of after_create, after_update, after_save callbacks
+# fix thread cache key for dynamic class methods
+
+version 0.1.5
+# naming fix
+
+version 0.1.4
+# cache key bug fix
+
+version 0.1.3
+# dependency updates to make it usable for all activerecord >3.0.0
+
+version 0.1.0
+# 2012-08-25 Go public
+# 2012-08-24 Added some tests
+# 2012-08-23 Added thread safe cache method KeyGen.global_cache_key
+
+version 0.0.1 (private)
+# 2012-07-14 First release for http://videofork.com/
+# YYYY-MM-DD Date format
17 Gemfile
@@ -0,0 +1,17 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in find_cache.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
+
+# jquery-rails is used by the dummy application
+#gem "jquery-rails"
+
+# Declare any dependencies that are still in development here instead of in
+# your gemspec. These might include edge Rails or gems from your path or
+# Git. Remember to move these dependencies to your gemspec before releasing
+# your gem to rubygems.org.
+
+# To use debugger
+# gem 'debugger'
@@ -0,0 +1,99 @@
+PATH
+ remote: .
+ specs:
+ find_cache (0.2.1)
+ activerecord (>= 3.0.0)
+ activesupport (>= 3.0.0)
+ simple_uuid (>= 0.2.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.0)
+ dalli (2.1.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.4)
+ json (1.7.5)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
+ multi_json (1.3.6)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
+ bundler (~> 1.0)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ simple_uuid (0.2.0)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.6)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.33)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ dalli (~> 2.1.0)
+ find_cache!
+ rails (~> 3.2.0)
+ sqlite3
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Mustafa TURAN
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Mustafa TURAN
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
+== FindCache
+
+It is simply an ActiveRecord object caching gem using Rails.cache methods.
+
+It makes "ActiveRecord 'find_by_id, find_by_(attr)' methods and 'has_one, belongs_to' relations" cacheable by PrimaryKey(PK) or any referenced columns using Rails.cache methods. It also supports fetching multiple records using PKs(ids) with find_all_cache method(if cache store supports multiple reads [hint: memcached_store, dalli_store supports.]).
+
+== Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'find_cache'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install find_cache
+
+== For has_one and belongs_to relations
+
+### Sample Models:
+
+ class User < ActiveRecord::Base
+ has_one :user_detail
+
+ # to enable has_one caching
+ find_cache_has_one :user_detail, UserDetail, :user_id
+ end
+
+ class UserDetail < ActiveRecord::Base
+ belongs_to :user
+
+ # to enable belongs_to caching
+ find_cache_belongs_to :user, User, :user_id
+ end
+
+== For finding a record
+
+ user = User.find_cache(id) # fetches from cache
+ user.user_detail
+ # fetches from cache if the User model has
+ # 'find_cache_has_one :user_detail, UserDetail, :user_id'
+
+ user = User.find_by_id(id) # fetches from DB
+ user.user_detail # fetches from DB
+
+== For fetching multiple ids
+
+ users = User.find_all_cache([1,2,3,4,5])
+
+== For fetching a record by attribute
+
+ user_detail = UserDetail.find_by_user_id(1) # from db
+ user_detail = UserDetail.find_cache_by_ref(:user_id, 1) # from cache store
+
+== For counter_cache 'cache invalidation'
+
+### Create a ruby file under config/initializers and add the following codes:
+
+ module ActiveRecord
+ module CounterCache
+ def increment_counter(counter_name, id)
+ expire_find_cache(name, id)
+ update_counters(id, counter_name => 1)
+ end
+
+ def decrement_counter(counter_name, id)
+ expire_find_cache(name, id)
+ update_counters(id, counter_name => -1)
+ end
+
+ def expire_find_cache(name, id)
+ key = FindCache::KeyGen.cache_key(name, id)
+ Rails.cache.delete(key)
+ $find_cache_store[FindCache::KeyGen.global_cache_key].delete(key)
+ end
+ end
+ end
+
+== Notes
+
+If your worker thread does not kill itself after execution you should clean the find_cache_store manually. It can be done simple in ApplicationController of your rails app adding this method as after_filter
+
+ after_filter :clear_find_cache_store
+
+ def clear_find_cache_store
+ FindCache::KeyGen.clear_find_cache_store
+ end
+
+Tested with dalli (https://github.com/mperham/dalli).
+
+== Todo
+
+More tests!
+
+== Credits
+
+FindCache is part of http://videofork.com project.
+
+== Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,38 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'FindCache'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
@@ -0,0 +1,28 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+
+# Maintain your gem's version:
+require "find_cache/version"
+
+# Describe your gem and declare its dependencies:
+Gem::Specification.new do |s|
+ s.name = "find_cache"
+ s.version = FindCache::VERSION
+ s.authors = ["Mustafa Turan"]
+ s.email = ["mustafaturan.net@gmail.com"]
+ s.homepage = "https://github.com/mustafaturan/find_cache"
+ s.summary = "Cache for ActiveRecord >=3.0.0 find_by_PrimaryKey(id) and find_by_(attr) methods based on Rails.cache methods."
+ s.description = "Makes ActiveRecord >=3.0.0 'find_by_id, find_by_(attr)' methods and 'has_one, belongs_to relations' cacheable by PK(id) or any referenced columns using Rails.cache methods. It also supports fetching multiple records using PKs(ids) with find_all_cache method(if cache store supports multiple reads [hint: memcache_store, dalli_store support.])."
+
+ s.files = Dir["{app,config,db,lib}/**/*"] + ["CHANGE-LOG", "MIT-LICENSE", "Rakefile", "README.rdoc"]
+ s.test_files = Dir["test/**/*"]
+
+
+ s.add_dependency "activerecord", [">= 3.0.0"]
+ s.add_dependency "activesupport", [">= 3.0.0"]
+ s.add_dependency "simple_uuid", [">= 0.2.0"]
+
+ s.add_development_dependency "rails", "~> 3.2.0"
+ s.add_development_dependency "sqlite3"
+ s.add_development_dependency "dalli", "~> 2.1.0"
+end
@@ -0,0 +1,6 @@
+# -*- encoding: utf-8 -*-
+require "active_support"
+require "active_record"
+require "find_cache/init"
+require "find_cache/key_gen"
+require "find_cache/cacheable"
Oops, something went wrong.

0 comments on commit 042d3fa

Please sign in to comment.