JRuby Rack question....is the java request + session available globally?

Hi All-

I’ve got a rails app I’m converting to jruby/warbler that uses Memcache
to
cache some web service calls. I’d like to remove that dependency to
Memcache.

To that end, I’m trying to change the cache location from memcache to
the
java session in a rails plugin I’ve written. Here is the relevant code:
def cached( key, &block )
key="#{key}"
request = java.servlet_request
session = request.getSession()
$servlet_context.log(“session: #{session}”)
cached_obj = session.getAttribute( key )
$servlet_context.log(“cached object: #{cached_obj}”)
if(cached_obj.nil? || cached_obj[:time].nil? || (cached_obj[:time] >
(Time.now - 5.minutes)))
cached_obj = {:time=>Time.now, :data=>block.call }
#@CACHE.add key, cached_obj, 300
session[key] = cached_obj
end
cached_obj[:data]
end

The line it has issues with is " cached_obj = session.getAttribute( key
)".
I get this error: ArgumentError (wrong number of arguments (1 for 0))

Am I correct to assume that jruby rack makes the java request and
session
globally available? If so, what am I doing wrong?

thanks for any suggestions
Mike Kohout

On Mon, Oct 6, 2008 at 2:18 PM, Michael K. [email protected]
wrote:

request = java.servlet_request
cached_obj[:data]

end

The line it has issues with is " cached_obj = session.getAttribute( key )".
I get this error: ArgumentError (wrong number of arguments (1 for 0))

Am I correct to assume that jruby rack makes the java request and session
globally available? If so, what am I doing wrong?

Request and session actually are not globally available, but the
servlet context is, as you have above. You should be able to use the
$servlet_context.get/setAttribute as a in-memory cache store.

The alternative is to make the servlet request and/or session
parameters to the #cached method, if you need a per-user session
cache.

Cheers,
/Nick


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

The servlet_request call will fail if you call it inside view files (or
indirectly, calling your cached method inside views). You can fix this
making the servlet_request method available inside views as a helper
method:
class ApplicationController < ActionController::Base

helper_method :servlet_request

end

On Mon, Oct 6, 2008 at 4:18 PM, Michael K. [email protected]
wrote:

request = java.servlet_request
cached_obj[:data]


Fabio K.

Caelum - Ensino e Inovação
http://www.caelum.com.br

On Tue, Oct 7, 2008 at 10:25 AM, Michael K. [email protected]
wrote:

Nick, Fabio, thanks for the suggestions.

My biggest trouble is that the web service I’m calling is called in Models
and Controllers(it’s an access control thing), so I’d rather avoid passing
the session around averywhere.

This is more of a ruby question(and shows a bit of greenness), but is there
a way to bind the session or request to a dynamically scoped variable, so
that it’s bound in the init method of the controller (and visible from a
plugin)?

Ruby doesn’t have dynamically-scoped variables, if that’s what you’re
thinking (a variable created in a parent stack frame visible down the
call stack). With Fabio’s suggestion, the servlet_request method will
be available in controllers and views. You’ll have to pass it as a
parameter into model methods to use it there.

/Nick


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Perhaps, it would be better to pass what you want from the session,
instead
of passing the session itself.

On Tue, Oct 7, 2008 at 1:19 PM, Nick S. [email protected]
wrote:

This is more of a ruby question(and shows a bit of greenness), but is

/Nick


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Fabio K.

Caelum - Ensino e Inovação
http://www.caelum.com.br

Nick, Fabio, thanks for the suggestions.

My biggest trouble is that the web service I’m calling is called in
Models
and Controllers(it’s an access control thing), so I’d rather avoid
passing
the session around averywhere.

This is more of a ruby question(and shows a bit of greenness), but is
there
a way to bind the session or request to a dynamically scoped variable,
so
that it’s bound in the init method of the controller (and visible from a
plugin)?

thanks
Mike Kohout

Thanks for your help, everybody.

Rather than change tons of code, for now, I changed things to just store
everything inside a static map…the performance kinda sucks, but it
keeps
the code from any external dependencies.

On Mon, Oct 6, 2008 at 8:18 PM, Michael K. [email protected]
wrote:

Hi All-

I’ve got a rails app I’m converting to jruby/warbler that uses Memcache to
cache some web service calls. I’d like to remove that dependency to
Memcache.

I don’t know if this is relevant, but I read about this yesterday:


Rasputnik :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email