Permalink
Browse files

first post

  • Loading branch information...
0 parents commit 7b059349b40e567e31463f3b3c5883c60e765b65 @defunkt committed Jan 23, 2008
No changes.
18 LICENSE
@@ -0,0 +1,18 @@
+Copyright (c) 2007 Chris Wanstrath
+
+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.
17 README
@@ -0,0 +1,17 @@
+== cache_fu
+
+A rewrite of acts_as_cached.
+
+== Changes from acts_as_cached 1
+
+- You can no longer set a 'ttl' method on a class. Instead,
+ pass :ttl to acts_as_cached:
+ >> acts_as_cached :ttl => 15.minutes
+
+- The is_cached? method is aliased as cached?
+
+- set_cache on an instance can take a ttl
+ >> @story.set_cache(15.days)
+
+
+Chris Wanstrath [ chris[at]ozmm[dot]org ]
@@ -0,0 +1,42 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'load_multi_rails_rake_tasks'
+
+desc "Run all the tests"
+task :default => :test
+
+test_files = FileList['test/*test.rb']
+
+desc 'Test the cache_fu plugin.'
+task :test do
+ test_files.each do |file|
+ ruby "#{file}"
+ end
+end
+
+desc 'Test the cache_fu plugin against Rails 1.2.5'
+task :test_with_125 do
+ ENV['MULTIRAILS_RAILS_VERSION'] = '1.2.5'
+ test_files.each do |file|
+ ruby "#{file}"
+ end
+end
+
+desc "Run cache_fu tests using a memcache daemon"
+task :test_with_memcache do
+ test_files.each do |file|
+ ruby "#{file} with-memcache"
+ end
+end
+
+desc 'Generate RDoc documentation for the cache_fu plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ files = ['README', 'LICENSE', 'lib/**/*.rb']
+ rdoc.rdoc_files.add(files)
+ rdoc.main = "README" # page to start on
+ rdoc.title = "cache_fu"
+ rdoc.template = File.exists?(t="/Users/chris/ruby/projects/err/rock/template.rb") ? t : "/var/www/rock/template.rb"
+ rdoc.rdoc_dir = 'doc' # rdoc output folder
+ rdoc.options << '--inline-source'
+end
@@ -0,0 +1,40 @@
+##
+# Copy this file to vendor/plugins/acts_as_cached/extensions.rb if you
+# wish to extend acts_as_cached with your own instance or class methods.
+#
+# You can, of course, do this directly in your cached classes,
+# but keeping your custom methods here allows you to define
+# methods for all cached objects DRYly.
+module ActsAsCached
+ module Extensions
+ module ClassMethods
+ ##
+ # All acts_as_cached classes will be extended with
+ # this method.
+ #
+ # >> Story.multi_get_cache(13, 353, 1231, 505)
+ # => [<Story:13>, <Story:353>, ...]
+ def multi_get_cache(*ids)
+ ids.flatten.map { |id| get_cache(id) }
+ end
+ end
+
+ module InstanceMethods
+ ##
+ # All instances of a acts_as_cached class will be
+ # extended with this method.
+ #
+ # => story = Story.get_cache(1)
+ # => <Story:1>
+ # >> story.reset_included_caches
+ # => true
+ def reset_included_caches
+ return false unless associations = cache_config[:include]
+ associations.each do |association|
+ Array(send(association)).each { |item| item.reset_cache }
+ end
+ true
+ end
+ end
+ end
+end
@@ -0,0 +1,34 @@
+defaults:
+ ttl: 1800
+ readonly: false
+ urlencode: false
+ c_threshold: 10000
+ compression: true
+ debug: false
+ namespace: app
+ sessions: false
+ session_servers: false
+ fragments: false
+ memory: 64
+ servers: localhost:11211
+ benchmarking: true
+ raise_errors: true
+ fast_hash: false
+ fastest_hash: false
+
+development:
+ sessions: false
+ fragments: false
+ servers: localhost:11211
+
+# turn off caching
+test:
+ disabled: true
+
+production:
+ memory: 256
+ benchmarking: false
+ servers:
+ - 192.185.254.121:11211
+ - 192.185.254.138:11211
+ - 192.185.254.160:11211
@@ -0,0 +1,80 @@
+#!/usr/bin/env ruby
+# By atmos@atmos.org
+# this goes in your script/ directory
+# it parses your memcached.yml file and hooks you up w/ some info
+# it keeps you from having to mess w/ stale memcached daemons for whatever reason.
+require 'yaml'
+require 'timeout'
+
+class MemcachedCtl
+ attr_accessor :memcached, :memory, :pids, :servers, :ip_address, :ethernet_device
+
+ def initialize
+ env = ENV['RAILS_ENV'] || 'development'
+ self.memcached = `which memcached`.chomp
+ self.servers = [ ]
+ self.pids = { }
+ self.ethernet_device = ENV['ETH'] || 'eth0'
+ self.ip_address = get_ip_address || '0.0.0.0'
+ self.memory = '128'
+
+ config = YAML.load_file(File.expand_path(File.dirname(__FILE__) + "/../config/memcached.yml"))
+ self.servers = [ config['defaults']['servers'] ].flatten rescue ['127.0.0.1:11211']
+ self.servers = [ config[env]['servers'] ].flatten if config[env]['servers']
+ self.servers.reject! { |server| host,port = server.split(/:/); self.ip_address == host }
+ self.memory = config[env]['memory'] unless config[env]['memory'].nil?
+
+ each_server do |host,port|
+ `ps auwwx | grep memcached | grep '\\-l #{ip_address} \\-p #{port}' | grep -v grep`.split(/\n/).each do |line|
+ self.pids[port] = line.split(/\s+/)[1]
+ end
+ self.pids[port] ||= 'Down'
+ end
+ end
+
+ def execute(cmd)
+ send(cmd) rescue usage
+ end
+
+ def restart; stop; sleep 1; start end
+
+ def status
+ each_server { |host,port| puts "Port #{port} -> #{pids[port] =~ /\d+/ ? 'Up' : 'Down'}" }
+ end
+
+ def kill
+ each_server { |host,port| `kill -9 #{pids[port]} > /dev/null 2>&1` if pids[port] =~ /\d+/ }
+ end
+
+ def stop; kill end
+
+ def start
+ each_server do |host,port|
+ `#{memcached} -d -m #{memory} -l #{ip_address} -p #{port}`
+ STDERR.puts "Try memcached_ctl status" unless $? == 0
+ end
+ end
+
+ def usage
+ methods = %w[start stop restart kill status]
+ puts "Usage: script/memcached_ctl [ " + (methods * ' | ') + " ]"
+ end
+
+protected
+ def each_server
+ servers.each do |server|
+ host, port = server.split(/:/)
+ yield host, port
+ end
+ end
+
+ def get_ip_address # this works on linux you might have to tweak this on other oses
+ line = `/sbin/ifconfig #{ethernet_device} | grep inet | grep -v inet6`.chomp
+ if line =~ /\s*inet addr:((\d+\.){3}\d+)\s+.*/
+ self.ip_address = $1
+ end
+ end
+end
+###########################################################################
+
+MemcachedCtl.new.execute(ARGV.first)
27 init.rb
@@ -0,0 +1,27 @@
+require 'memcache'
+puts "=> You should be using the `memcache-client' gem. You're using RubyMemcache!" if Object.const_defined?(:RubyMemcache)
+
+begin
+ require 'mem_cache_with_consistent_hashing'
+rescue LoadError
+else
+ puts "=> MemCacheWithConsistentHashing being used."
+end
+
+require 'acts_as_cached'
+
+Object.send :include, ActsAsCached::Mixin
+
+unless File.exists? config_file = File.join(RAILS_ROOT, 'config', 'memcached.yml')
+ error = "No config file found. Make sure you used `script/plugin install' and have memcached.yml in your config directory."
+ puts error
+ logger.error error
+ exit!
+end
+
+ActsAsCached.config = YAML.load(ERB.new(IO.read(config_file)).result)
+
+begin
+ require 'extensions'
+rescue LoadError
+end
@@ -0,0 +1,58 @@
+##
+# Do some checks.
+puts
+
+$errors = 0
+
+puts "** Checking for memcached in path..."
+if `which memcached`.strip.empty?
+ $errors += 1
+ puts "!! Couldn't find memcached in your path. Are you sure you installed it? !!"
+ puts "!! Check the README for help. You can't use acts_as_cached without it. !!"
+end
+
+puts "** Checking for memcache-client gem..."
+begin
+ require 'rubygems'
+ require 'memcache'
+rescue LoadError
+ $errors += 1
+ puts "!! Couldn't find memcache-client gem. You can't use acts_as_cached without it. !!"
+ puts "!! $ sudo gem install memcache-client !!"
+end
+
+require 'fileutils'
+def copy_file(in_file, out_file)
+ puts "** Trying to copy #{File.basename(in_file)} to #{out_file}..."
+ begin
+ if File.exists? out_file
+ puts "!! You already have a #{out_file}. " +
+ "Please check the default for new settings or format changes. !!"
+ puts "!! You can find the default at #{in_file}. !!"
+ $errors += 1
+ else
+ FileUtils.cp(in_file, out_file)
+ end
+ rescue
+ $errors += 1
+ puts "!! Error copying #{File.basename(in_file)} to #{out_file}. Please try by hand. !!"
+ end
+end
+
+defaults_dir = File.join(File.dirname(__FILE__), 'defaults')
+
+config_yaml = File.join('.', 'config', 'memcached.yml')
+default_yaml = File.join(defaults_dir, 'memcached.yml.default')
+copy_file(default_yaml, config_yaml)
+
+memcached_ctl = File.join('.', 'script', 'memcached_ctl')
+default_ctl = File.join(defaults_dir, 'memcached_ctl.default')
+copy_file(default_ctl, memcached_ctl)
+
+puts
+print $errors.zero? ? "**" : "!!"
+print " acts_as_cached installed with #{$errors.zero? ? 'no' : $errors} errors."
+print " Please edit the memcached.yml file to your liking."
+puts $errors.zero? ? "" : " !!"
+puts "** Now would be a good time to check out the README. Enjoy your day."
+puts
@@ -0,0 +1,50 @@
+require 'acts_as_cached/config'
+require 'acts_as_cached/cache_methods'
+require 'acts_as_cached/fragment_cache'
+require 'acts_as_cached/benchmarking'
+require 'acts_as_cached/disabled'
+require 'acts_as_cached/local_cache'
+
+module ActsAsCached
+ @@config = {}
+ mattr_reader :config
+
+ def self.config=(options)
+ @@config = Config.setup options
+ end
+
+ def self.skip_cache_gets=(boolean)
+ ActsAsCached.config[:skip_gets] = boolean
+ end
+
+ module Mixin
+ def acts_as_cached(options = {})
+ extend ClassMethods
+ include InstanceMethods
+
+ extend Extensions::ClassMethods if defined? Extensions::ClassMethods
+ include Extensions::InstanceMethods if defined? Extensions::InstanceMethods
+
+ options.symbolize_keys!
+
+ options[:store] ||= ActsAsCached.config[:store]
+ options[:ttl] ||= ActsAsCached.config[:ttl]
+
+ # convert the find_by shorthand
+ if find_by = options.delete(:find_by)
+ options[:finder] = "find_by_#{find_by}".to_sym
+ options[:cache_id] = find_by
+ end
+
+ cache_config.replace options.reject { |key,| not Config.valued_keys.include? key }
+ cache_options.replace options.reject { |key,| Config.valued_keys.include? key }
+
+ Disabled.add_to self and return if ActsAsCached.config[:disabled]
+ Benchmarking.add_to self if ActsAsCached.config[:benchmarking]
+ end
+ end
+
+ class CacheException < StandardError; end
+ class NoCacheStore < CacheException; end
+ class NoGetMulti < CacheException; end
+end
Oops, something went wrong.

0 comments on commit 7b05934

Please sign in to comment.