Created
July 5, 2011 16:54
-
-
Save timuruski/1065257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'mongo_mapper' | |
| MongoMapper.connection = Mongo::Connection.new("127.0.0.1", 27017, | |
| :logger => Logger.new(STDOUT)) | |
| MongoMapper.database = 'tmp' | |
| class Example | |
| include MongoMapper::Document | |
| key :name, String, :unique => true | |
| def self.find_by_name(name) | |
| first(:name => name) | |
| end | |
| # Allows Memoizable to handle class-methods | |
| class << self | |
| extend ActiveSupport::Memoizable | |
| memoize :find_by_name | |
| end | |
| end | |
| Example.collection.drop | |
| Example.create(:name => 'foo') | |
| Example.create(:name => 'bar') | |
| puts "-- MEMOIZE" | |
| puts " - first call:" | |
| Example.find_by_name('foo') | |
| Example.find_by_name('bar') | |
| puts " - second call:" | |
| Example.find_by_name('foo') | |
| Example.find_by_name('bar') | |
| puts "\n-- WITH RELOAD ARG" | |
| puts " - first call:" | |
| Example.find_by_name('foo', :reload) | |
| Example.find_by_name('bar', :reload) | |
| puts " - second call:" | |
| Example.find_by_name('foo') | |
| Example.find_by_name('bar') | |
| puts "\n-- FLUSH CACHE" | |
| Example.flush_cache(:find_by_name) | |
| puts " - first call:" | |
| Example.find_by_name('foo') | |
| Example.find_by_name('bar') | |
| puts " second call:" | |
| Example.find_by_name('foo') | |
| Example.find_by_name('bar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment