Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rails 1.2.x doesn't have "LIMIT 1" on its simple finds.

[git-p4: depot-paths = "//src/cached_model/dev/": change = 3021]
  • Loading branch information...
commit fcdf08ae26d68af6705a2a61a5678ce1f047cfad 1 parent bfc6b78
@drbrain drbrain authored
Showing with 11 additions and 22 deletions.
  1. +2 −1  Rakefile
  2. +9 −21 lib/cached_model.rb
View
3  Rakefile
@@ -1,8 +1,9 @@
# vim: syntax=Ruby
require 'hoe'
+require './lib/cached_model'
-Hoe.new 'cached_model', '1.3.1' do |p|
+Hoe.new 'cached_model', CachedModel::VERSION do |p|
p.summary = 'An ActiveRecord abstract model that caches records in memcached'
p.description = 'CachedModel caches simple (by id) finds in memcached reducing the amount of work the database needs to perform for simple queries.'
p.author = ['Eric Hodel', 'Robert Cottrell']
View
30 lib/cached_model.rb
@@ -2,6 +2,7 @@
require 'timeout'
require 'memcache_util' unless $TESTING_CM
+require 'active_record' unless $TESTING_CM
##
# An abstract ActiveRecord descendant that caches records in memcache and in
@@ -20,6 +21,10 @@
class CachedModel < ActiveRecord::Base
+ self.abstract_class = true
+
+ VERSION = '1.3.2'
+
@cache_delay_commit = {}
@cache_local = {}
@cache_transaction_level = 0
@@ -71,24 +76,6 @@ class << self
end
##
- # We only work on 1.1.2 + because Rails broke backwards compatibility
- # despite a bug http://dev.rubyonrails.org/ticket/3704
-
- if Rails::VERSION::MAJOR > 1 or
- (Rails::VERSION::MAJOR == 1 and Rails::VERSION::MINOR > 1) or
- (Rails::VERSION::MAJOR == 1 and Rails::VERSION::MINOR == 1 and
- Rails::VERSION::TINY >= 2) then
-
- ##
- # Override the flawed assumption ActiveRecord::Base makes about
- # inheritance.
-
- self.abstract_class = true
- else
- raise NotImplementedError, 'upgrade to Rails 1.1.2+'
- end
-
- ##
# Invalidate the cache entry for a record. The update method will
# automatically invalidate the cache when updates are made through
# ActiveRecord model record. However, several methods update tables with
@@ -146,7 +133,7 @@ def self.find(*args)
# Find by primary key from the cache.
def self.find_by_sql(*args)
- return super unless args.first =~ /^SELECT \* FROM #{table_name} WHERE \(#{table_name}\.#{primary_key} = '?(\d+)'?\) +LIMIT 1/
+ return super unless args.first =~ /^SELECT \* FROM #{table_name} WHERE \(#{table_name}\.#{primary_key} = '?(\d+)'?\)/
id = $1.to_i
@@ -269,7 +256,8 @@ def cache_local
def cache_store
obj = dup
obj.send :instance_variable_set, :@attributes, attributes_before_type_cast
- if CachedModel.cache_delay_commit[CachedModel.cache_transaction_level].nil? then
+ transaction_level = CachedModel.cache_transaction_level
+ if CachedModel.cache_delay_commit[transaction_level].nil? then
if CachedModel.use_local_cache? then
cache_local[cache_key_local] = obj
end
@@ -277,7 +265,7 @@ def cache_store
Cache.put cache_key_memcache, obj, CachedModel.ttl
end
else
- CachedModel.cache_delay_commit[CachedModel.cache_transaction_level] << obj
+ CachedModel.cache_delay_commit[transaction_level] << obj
end
nil
end
Please sign in to comment.
Something went wrong with that request. Please try again.