ahawkins / cashier

Tag based caching for Rails using Redis. Associate different cached content with a tag, then expire by tag instead of key.
MIT License
162 stars 39 forks source link

uninitialized class variable @@adapter in Cashier #11

Open constantm opened 11 years ago

constantm commented 11 years ago

I'm trying to use this Gem with no luck - I can't figure out from the documentation if I'm using it correctly.

I have an products index, with various combinations of query strings. I would like to cache these all under a tag, and then clear all cached items in a tag when a product is changed, removed or added.

In my Products controller I have:

class ProductsController < ApplicationController
   caches_action :index, :cache_path => Proc.new { |c| c.params }, :tag => "products"
   cache_sweeper :product_sweeper #Products sweeper will use Cashier.expire "products" if needed

   def index
      # rest of code
   end
end

When trying to use Cashier as above, I get the following:

uninitialized class variable @@adapter in Cashier

What am I doing wrong? Any help would be greatly appreciated.

ahawkins commented 11 years ago

open rails the console and run: Cashier.adapter. What does it say?

constantm commented 11 years ago
Loading development environment (Rails 3.2.6)
irb(main):001:0> Cashier.adapter
NameError: uninitialized class variable @@adapter in Cashier
    from /usr/local/opt/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/cashier-0.4.0/lib/cashier.rb:7:in `adapter'
    from (irb):1
    from /usr/local/opt/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/opt/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/opt/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0> 
constantm commented 11 years ago

Any feedback?

lukes commented 11 years ago

This baffled me for a long time, but I've got it to work now. Instead of following the documentation in this Github repo, create a new initializer and add

Cashier.adapter = :redis_store
Cashier.adapter.redis = Redis.new(:host => '127.0.0.1', :port => '3697')  # or, actually Resque.redis in our case

That's for Redis, you can set Cashier.adapter to whatever store you were wanting to use.

constantm commented 11 years ago

Thanks @lukes - I ended up not using Cashier, but will try it again in the future.