Setting database connection for Ramaze::Cache::Sequel

129 views
Skip to first unread message

Lars Olsson

unread,
Apr 9, 2011, 6:10:50 AM4/9/11
to Ramaze
Hi list!

Just a quick question about the Sequel Cache class
(Ramaze::Cache::Sequel). How am I supposed to set what database
connection to use? The Ramaze book shows how to use a different cache
class, but not how to set any options for the class that you want to
use. And the source code for the Sequel cache does not contain any
references to any database connection either. Where is this setting
stored and how do I set it?

/lasso

Yorick Peterse

unread,
Apr 9, 2011, 9:14:50 AM4/9/11
to ram...@googlegroups.com
Lars,

It just uses the main Sequel connection created with Sequel.connect(). I'm not sure how it would handle multiple database connections (e.g. a SQLite and PostgreSQL database) at the same time.

Yorick


--
You received this message because you are subscribed to the Google Groups "Ramaze" group.
To post to this group, send email to ram...@googlegroups.com.
To unsubscribe from this group, send email to ramaze+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ramaze?hl=en.


Lars Olsson

unread,
Apr 9, 2011, 12:27:14 PM4/9/11
to Ramaze
By "the main sequel connection", do you mean the *magic* DB variable
the Sequel automatically looks for?

Sequel supports multiple connections for different models/datasets.
Here is a small sample from the file I'm using to define the objects
in my blog

...
BLOG_DB = Sequel.sqlite('data/blog/blog.db')
COMMENTS_DB = Sequel.sqlite('data/blog/comments.db')

class Blog < Sequel::Model
self.db = BLOG_DB
...
end

...

class BlogComment < Sequel::Model
self.db = COMMENTS_DB
...
end

...this could also be written like: class MyModel <
Sequel::Model(my_db_connection_var)

Also, the cache_setup method takes some arguments. I assume that
cache_setup gets called when Ramaze starts, but is it possible to
affect the parameters sent somehow (via Ramaze.options or similar)?

/lasso

Yorick Peterse

unread,
Apr 9, 2011, 3:14:23 PM4/9/11
to ram...@googlegroups.com
Lars,

Yes, it uses the "magic" variable. Currently it's not possible to specify an alternative database connection in the Table class.
What you could do is the following:

    module Ramaze
      class Cache
        class Sequel
          class Table < Sequel::Model(:ramaze_cache)
            self.db = AlternativeDB
          end
        end
      end
    end

This should be done before telling Ramaze to use the Sequel cache and it should work (haven't tested this). Of course this
isn't an ideal solution so patches are more than welcome :)

Yorick

Lars Olsson

unread,
Apr 10, 2011, 6:10:14 AM4/10/11
to Ramaze
Alright, I'll provide a proposed patch during the coming week.

/lasso

Lars Olsson

unread,
Apr 13, 2011, 4:17:45 PM4/13/11
to Ramaze
Alright, here comes a first stab of fixing the Sequel cache.

Patch against current ramaze at: http://www.lassoweb.se/tmp/ramaze_sequel_cache.diff

I've mainly changed two things from the current Sequel Cache. The
first thing is that I have added the possibility to use options when
setting the cache. Like this:

# For default options (same as before)
Ramaze.options.cache.session = Ramaze::Cache::Sequel

# For custom options
Ramaze.options.cache.session = Ramaze::Cache::Sequel.using(
:connection => Sequel.mysql(
:host=>'localhost', :user=>'user',
:password=>'password', :database=>'blog'
),
:table => :blog_sessions
)

This will create a mysql cache in database 'blog' in table
'blog_sessions'. Each cache will can also have its own settings, so
it's possible to use two different tables (or even different database
adapters) for two different caches. The options array can also control
some other things, but please check out the patch for that.

The second thing I've done is to strip out all of Sequel::Model from
the cache class. Since we're trying to be as fast as possible (it's a
cache after all) I felt it was important to remove as much fancy stuff
as possible. The whole class now uses raw Sequel datasets instead.
I've borrowed parts of Sequels serialize plugin in order to make
serialization smooth, but the cache now handles all of it's tasks by
itself. No need for any Sequel plugins anymore.

Since its my first patch in quite a while I would be very happy to
accept suggestions on how to improve it. When it's good enough I hope
it will find it's way into the "official" Ramaze.

/lasso

Yorick Peterse

unread,
Apr 13, 2011, 5:05:51 PM4/13/11
to ram...@googlegroups.com
Lars,

I took a look at your diff (and the code when applying it as a patch) and I have to say it looks absolutely wonderful. 
I sent the link to this thread to Michael and we'll see what he has to say about it :)

Yorick

Yorick Peterse

unread,
Apr 21, 2011, 9:23:36 AM4/21/11
to ram...@googlegroups.com
Lars,

As of this day your patch (although slightly modified to meet the standards set by Ramaze/Michael) is part of Ramaze itself :)

Yorick

Lars Olsson

unread,
Apr 21, 2011, 10:34:21 AM4/21/11
to Ramaze
Nice! :)

I've actually never considered using traits myself, but if that's the
way to go for Ramaze stuff I'll use them in future patches.

/lasso



On 21 Apr, 15:23, Yorick Peterse <i...@yorickpeterse.com> wrote:
> Lars,
>
> As of this day your patch (although slightly modified to meet the standards
> set by Ramaze/Michael) is part of Ramaze itself :)
>
> Yorick
>

Yorick Peterse

unread,
Apr 21, 2011, 10:55:37 AM4/21/11
to ram...@googlegroups.com
Lars,

It's not the de-facto way of adding configuration options as usually Innate::Optioned is used for that. However, in this case there was the need of instance specific options, something Innate::Optioned can't do.
Because of this Traits were used :)

Yorick

Lars Olsson

unread,
Apr 21, 2011, 11:01:12 AM4/21/11
to Ramaze
Hello,

when checking out the updated patch I actually discoved two small
mistakes I made during development. One is a unused variable, the
other is a small error in the documentation. No hurry to fix it, but
the diff (affecting only three loc) is available at
http://www.lassoweb.se/tmp/ramaze_sequel_cache-v2.diff

/lasso

Yorick Peterse

unread,
Apr 21, 2011, 1:59:32 PM4/21/11
to ram...@googlegroups.com
Lars,


Yorick
Reply all
Reply to author
Forward
0 new messages