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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael K. (Guest)
on 2008-10-06 23:18
(Received via mailing list)
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
Nick S. (Guest)
on 2008-10-07 03:08
(Received via mailing list)
On Mon, Oct 6, 2008 at 2:18 PM, Michael K. 
<removed_email_address@domain.invalid>
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
Fabio K. (Guest)
on 2008-10-07 19:12
(Received via mailing list)
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. 
<removed_email_address@domain.invalid>
wrote:

>     request = java.servlet_request
>     cached_obj[:data]
>
--
Fabio K.
http://www.fabiokung.com

Caelum - Ensino e Inovação
http://www.caelum.com.br
Michael K. (Guest)
on 2008-10-07 19:36
(Received via mailing list)
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
Nick S. (Guest)
on 2008-10-07 20:26
(Received via mailing list)
On Tue, Oct 7, 2008 at 10:25 AM, Michael K. 
<removed_email_address@domain.invalid>
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
Fabio K. (Guest)
on 2008-10-07 21:46
(Received via mailing list)
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. <removed_email_address@domain.invalid>
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.
http://www.fabiokung.com

Caelum - Ensino e Inovação
http://www.caelum.com.br
Dick D. (Guest)
on 2008-10-07 21:50
(Received via mailing list)
On Mon, Oct 6, 2008 at 8:18 PM, Michael K. 
<removed_email_address@domain.invalid>
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:

http://www.infoq.com/news/2008/10/jgroups-memcached

--
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
Michael K. (Guest)
on 2008-10-08 02:39
(Received via mailing list)
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.
This topic is locked and can not be replied to.