Skip to content

Commit

Permalink
Support off-heap store and distributed caching.
Browse files Browse the repository at this point in the history
Ruby objects stored in Ehcache will be automatically marshaled to a Java byte
array if the cache they are being placed in is clustered or the off-heap store
is in use. On retrieval, such Ruby objects are automatically unmarshaled before
being returned to the client. This enables the use of BigMemory and Terracotta
clustered Ehcache.
  • Loading branch information
jvoegele committed Mar 20, 2012
1 parent 67c9a76 commit 182395f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Binary file added ext/marshaled-ruby-object.jar
Binary file not shown.
12 changes: 11 additions & 1 deletion lib/ehcache/cache.rb
Expand Up @@ -10,6 +10,13 @@ def each
yield self.get(key)
end
end

# Does this cache require marshalling of Ruby objects?
def marshal?
config = self.cache_configuration
config.overflow_to_disk? || config.overflow_to_off_heap? || config.terracotta_clustered?
end

# Gets an element value from the cache. Unlike the #get method, this method
# returns the element value, not the Element object.
def [](key)
Expand All @@ -26,7 +33,10 @@ def put(*args)
if args.size == 1 && args.first.kind_of?(Ehcache::Element)
element = args.first
elsif args.size == 2
element = Ehcache::Element.create(args[0], args[1], options)
if marshal?
value = Java::NetSfEhcache::MarshaledRubyObject.new(Marshal.dump(args[1]).to_java_bytes)
end
element = Ehcache::Element.create(args[0], value, options)
else
raise ArgumentError, "Must be Element object or key and value arguments"
end
Expand Down
12 changes: 12 additions & 0 deletions lib/ehcache/element.rb
Expand Up @@ -9,6 +9,18 @@ def self.create(key, value, options = {})
result
end

alias element_value value

# Wrap the Element#value method to unmarshal Ruby objects if necessary.
def value
val = element_value
if val.kind_of?(Java::NetSfEhcache::MarshaledRubyObject)
Marshal.load(String.from_java_bytes(val.bytes))
else
val
end
end

alias tti getTimeToIdle
alias ttl getTimeToLive

Expand Down
15 changes: 15 additions & 0 deletions src/net/sf/ehcache/MarshaledRubyObject.java
@@ -0,0 +1,15 @@
package net.sf.ehcache;

import java.io.Serializable;

public class MarshaledRubyObject implements Serializable {
private byte[] bytes;

public MarshaledRubyObject(byte[] bytes) {
this.bytes = bytes;
}

public byte[] getBytes() {
return this.bytes;
}
}

0 comments on commit 182395f

Please sign in to comment.