I’m trying to set up ehcache with JRuby Rails. Storing a Java object in ehcache works fine, but retrieving it gives me some trouble. If I trace out the object that ehcache returns me I get this:
[ key = mipsObject, value=ProxyBridge@116fe10, version=1, hitCount=1,
CreationTime = 1330679995356, LastAccessTime = 1330679995357 ]
Now, how can I access the value?
I’m not really sure whether this is a java object being traced out or a ruby object…
Code:
def store_object(obj)
EHCACHE.put(“obj”, obj)
end
def get_object
EHCACHE.get(“obj”) #gives me the above object. It should only return its value
end
The Ehcache.get method returns an Ehcache Element object that contains the value as well as some metadata. If you want to obtain the actual value you have two options.
One is that you can call the getValue() method on the Element object returned by Ehcache.get(). JRuby allows you to call this method as simply “value” if you prefer:
Second, instead of using the Ehcache.get() method, you can use array access notation to get the value directly. This is a bit of Ruby magic we added to the Ehcache API in the JRuby bindings:
Hope that helps.
You can read my blog post for more information on using Ehcache with JRuby:
http://jvoegele.blogspot.com/2010/11/ehcache-for-jruby-and-rails-now-with.html