Only I mentioned "memorystorage" before, but I meant
MemoryManager<https://svn.apache.org/repos/asf/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java>
(sorry,
I was writing from my cell phone) which is a singleton as
well. MemoryManager is wrapped by the Cache singleton - i.e (from
Cache.java):
public static Pointer putByteArray(String key, byte[] payload, int expiresIn) {
Pointer ptr = MemoryManager.store(payload, expiresIn);
map.put(key, ptr);
return ptr;
}
The Cache facade basically adds key->value indexing (in a guava map) and
transparent serialization of objects (with protostuff) to the MemoryManager.
MemoryManager, in turn, is the class that handles a list of
OffHeapMemoryBuffers acting like if it was just one (this is needed to
override the 2gb allocation limit for each DirectBuffer and useful to
allocate memory in chunks). i.e (from MemoryManager.java):
public static Pointer store(byte[] payload, int expiresIn) {
Pointer p = activeBuffer.store(payload, expiresIn);
if (p == null) {
if (activeBuffer.bufferNumber+1 == buffers.size()) {
return null;
} else {
// try next buffer
activeBuffer = buffers.get(activeBuffer.bufferNumber+1);
p = activeBuffer.store(payload, expiresIn);
}
}
return p;
}
OffHeapMemoryBuffer holds a single allocated DirectBuffer and a list of
Pointers. The Pointer structure holds just offset and length (the
coordinates) of the specific item into the DirectBuffer and also number of
hits and expiry information (needed to handle expiry and LFU/LRU eviction) -
plus, of course, a boolean stating whether the pointer is free. Every time
an item needs to be stored a new Pointer is "sliced" from the first free one
large enough to contain the new item. Freeing the buffer means simply
setting "free=true".
Sorry for being a bit longish but as I was writing I thought this
explanation could be a good starting point for a wiki page :)
Ciao,
R
On Thu, Oct 20, 2011 at 12:58 AM, Raffaele P. Guidi <
[email protected]> wrote:
> Exactly like that :)
>
>
> On Thu, Oct 20, 2011 at 12:34 AM, Ioannis Canellos <[email protected]>wrote:
>
>> I am not sure if I understand it correctly. So please correct me if I am
>> wrong.
>>
>> You mean that you want to keep Cache as a singleton, but maybe add
>> something
>> like CacheInstance which will not be a singleton?
>>
>> If I understand it right, we could then have Cache use a static reference
>> to
>> Cache instance and do the work, right?
>>
>> This is similar to how Hazelcast works and yes it would work for OSGi.
>> --
>> *Ioannis Canellos*
>> *
>> FuseSource <http://fusesource.com>
>>
>> **
>> Blog: http://iocanel.blogspot.com
>> **
>> Apache Karaf <http://karaf.apache.org/> Committer & PMC
>> Apache ServiceMix <http://servicemix.apache.org/> Committer
>> Apache Gora <http://incubator.apache.org/gora/> Committer
>> *
>>
>
>