Permalink
Browse files

find_cache public release

  • Loading branch information...
0 parents commit 11ef72efec14cb1dcd4daf217263be9294fd9f7d @mustafaturan committed Aug 24, 2012
Showing with 1,427 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +8 −0 CHANGE-LOG
  3. +17 −0 Gemfile
  4. +101 −0 Gemfile.lock
  5. +20 −0 MIT-LICENSE
  6. +80 −0 README.rdoc
  7. +38 −0 Rakefile
  8. +25 −0 find_cache.gemspec
  9. +5 −0 lib/find_cache.rb
  10. +88 −0 lib/find_cache/cacheable.rb
  11. +1 −0 lib/find_cache/init.rb
  12. +27 −0 lib/find_cache/key_gen.rb
  13. +3 −0 lib/find_cache/version.rb
  14. +4 −0 lib/tasks/find_cache_tasks.rake
  15. +261 −0 test/dummy/README.rdoc
  16. +7 −0 test/dummy/Rakefile
  17. +15 −0 test/dummy/app/assets/javascripts/application.js
  18. +13 −0 test/dummy/app/assets/stylesheets/application.css
  19. +3 −0 test/dummy/app/controllers/application_controller.rb
  20. +2 −0 test/dummy/app/helpers/application_helper.rb
  21. 0 test/dummy/app/mailers/.gitkeep
  22. 0 test/dummy/app/models/.gitkeep
  23. +6 −0 test/dummy/app/models/comment.rb
  24. +7 −0 test/dummy/app/models/post.rb
  25. +13 −0 test/dummy/app/models/user.rb
  26. +14 −0 test/dummy/app/views/layouts/application.html.erb
  27. +4 −0 test/dummy/config.ru
  28. +59 −0 test/dummy/config/application.rb
  29. +10 −0 test/dummy/config/boot.rb
  30. +25 −0 test/dummy/config/database.yml
  31. +5 −0 test/dummy/config/environment.rb
  32. +39 −0 test/dummy/config/environments/development.rb
  33. +67 −0 test/dummy/config/environments/production.rb
  34. +37 −0 test/dummy/config/environments/test.rb
  35. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  36. +19 −0 test/dummy/config/initializers/counter_cache_patch.rb
  37. +15 −0 test/dummy/config/initializers/inflections.rb
  38. +5 −0 test/dummy/config/initializers/mime_types.rb
  39. +7 −0 test/dummy/config/initializers/secret_token.rb
  40. +8 −0 test/dummy/config/initializers/session_store.rb
  41. +14 −0 test/dummy/config/initializers/wrap_parameters.rb
  42. +5 −0 test/dummy/config/locales/en.yml
  43. +58 −0 test/dummy/config/routes.rb
  44. +10 −0 test/dummy/db/migrate/20120824210455_create_users.rb
  45. +11 −0 test/dummy/db/migrate/20120824210731_create_comments.rb
  46. +13 −0 test/dummy/db/migrate/20120824210927_create_posts.rb
  47. +43 −0 test/dummy/db/schema.rb
  48. 0 test/dummy/lib/assets/.gitkeep
  49. 0 test/dummy/log/.gitkeep
  50. +26 −0 test/dummy/public/404.html
  51. +26 −0 test/dummy/public/422.html
  52. +25 −0 test/dummy/public/500.html
  53. 0 test/dummy/public/favicon.ico
  54. +6 −0 test/dummy/script/rails
  55. +9 −0 test/dummy/test/fixtures/comments.yml
  56. +13 −0 test/dummy/test/fixtures/posts.yml
  57. +9 −0 test/dummy/test/fixtures/users.yml
  58. +7 −0 test/dummy/test/unit/comment_test.rb
  59. +7 −0 test/dummy/test/unit/post_test.rb
  60. +7 −0 test/dummy/test/unit/user_test.rb
  61. +51 −0 test/find_cache_test.rb
  62. +15 −0 test/test_helper.rb
@@ -0,0 +1,7 @@
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
+test/dummy/.sass-cache
@@ -0,0 +1,8 @@
+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,101 @@
+PATH
+ remote: .
+ specs:
+ find_cache (0.0.1)
+ dalli
+ rails (~> 3.2.8)
+ simple_uuid
+
+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)
+ jquery-rails (2.1.1)
+ railties (>= 3.1.0, < 5.0)
+ thor (~> 0.14)
+ 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
+ find_cache!
+ jquery-rails
+ sqlite3
@@ -0,0 +1,20 @@
+Copyright 2012 MUSTAFA TURAN
+
+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,80 @@
+# 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 id or any referenced columns using Rails.cache methods. It also supports fetching multiple records using 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:
+
+User (`has_one :user_detail`) ->
+`find_cache_has_one :user_detail, UserDetail, :user_id`
+`find_cache_has_one attr_name_or_any_other_name, ModelName, foreign_key`
+- email
+- username
+- password
+
+UserDetail (`belongs_to :user`) ->
+`find_cache_belongs_to :user, User, :user_id`
+`find_cache_belongs_to attr_name_or_any_other_name, ModelName, foreign_key`
+- user_id
+- avatar
+- age
+- sex
+- location
+
+## 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
+
+Tested with dalli (https://github.com/mperham/dalli).
+
+## 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,25 @@
+# -*- 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/findcache"
+ s.summary = "Cache for ActiveRecord find_by_id and find_by_(attr) methods."
+ s.description = "Makes ActiveRecord 'find_by_id, find_by_(attr)' methods and 'has_one, belongs_to relations' cacheable by id or any referenced columns using Rails.cache methods. It also supports fetching multiple records using ids with find_all_cache method(if cache store supports multiple reads [hint: memcache_store, dalli_store supports.])."
+
+ s.files = Dir["{app,config,db,lib}/**/*"] + ["CHANGE-LOG", "MIT-LICENSE", "Rakefile", "README.rdoc"]
+ s.test_files = Dir["test/**/*"]
+
+ s.add_dependency "rails", "~> 3.2.8"
+ s.add_dependency "simple_uuid"
+ s.add_dependency "dalli"
+
+ s.add_development_dependency "sqlite3"
+end
@@ -0,0 +1,5 @@
+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 11ef72e

Please sign in to comment.