com.caucho.server.distcache.CacheKeyManager Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2018 Caucho Technology -- all rights reserved
*
* This file is part of Resin(R) Open Source
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Resin Open Source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Resin Open Source is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Resin Open Source; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.server.distcache;
import java.io.OutputStream;
import java.security.MessageDigest;
import com.caucho.distcache.CacheSerializer;
import com.caucho.inject.Module;
import com.caucho.util.FreeList;
import com.caucho.util.HashKey;
import com.caucho.util.LruCache;
/**
* Manages the distributed cache
*/
@Module
public final class CacheKeyManager
{
private static final Object NULL_OBJECT = new Object();
private final CacheEntryManager _cacheEntryManager;
private FreeList _keyStreamFreeList
= new FreeList(32);
private final LruCache _keyCache;
CacheKeyManager(CacheEntryManager cacheEntryManager)
{
_cacheEntryManager = cacheEntryManager;
_keyCache = new LruCache(64 * 1024);
}
public final CacheEntryManager getCacheEntryManager()
{
return _cacheEntryManager;
}
/**
* Returns the key entry.
*/
public final DistCacheEntry getCacheEntry(Object key,
CacheHandle cache)
{
HashKey hashKey = createHashKey(key, cache.getConfig());
DistCacheEntry entry = _cacheEntryManager.createCacheEntry(hashKey, cache);
if (key != null)
entry.setKey(key);
return entry;
}
/**
* Returns the key entry.
*/
final public DistCacheEntry getCacheEntry(HashKey hashKey, CacheHandle cache)
{
return _cacheEntryManager.createCacheEntry(hashKey, cache);
}
final DistCacheEntry getLocalEntry(HashKey key, CacheHandle cache)
{
if (key == null)
throw new NullPointerException();
DistCacheEntry entry = getCacheEntry(key, cache);
return entry;
}
public void closeCache(String guid)
{
_keyCache.clear();
}
protected HashKey createHashKey(Object key, CacheConfig config)
{
CacheKey cacheKey = new CacheKey(config.getGuid(),
config.getGuidHash(),
key);
HashKey hashKey = _keyCache.get(cacheKey);
if (hashKey == null) {
hashKey = createHashKeyImpl(key, config);
_keyCache.put(cacheKey, hashKey);
}
return hashKey;
}
/**
* Returns the key hash
*/
protected HashKey createHashKeyImpl(Object key, CacheConfig config)
{
try {
KeyHashStream dOut = _keyStreamFreeList.allocate();
if (dOut == null) {
MessageDigest digest
= MessageDigest.getInstance(HashManager.HASH_ALGORITHM);
dOut = new KeyHashStream(digest);
}
dOut.init();
CacheSerializer keySerializer = config.getKeySerializer();
keySerializer.serialize(config.getGuid(), dOut);
keySerializer.serialize(key, dOut);
HashKey hashKey = new HashKey(dOut.digest());
_keyStreamFreeList.free(dOut);
return hashKey;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Returns the key hash
*/
public HashKey createSelfHashKey(Object key, CacheSerializer keySerializer)
{
try {
MessageDigest digest
= MessageDigest.getInstance(HashManager.HASH_ALGORITHM);
KeyHashStream dOut = new KeyHashStream(digest);
keySerializer.serialize(key, dOut);
HashKey hashKey = new HashKey(dOut.digest());
return hashKey;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public String toString()
{
return getClass().getSimpleName() + "[" + _cacheEntryManager + "]";
}
static final class CacheKey {
private final String _guid;
private final Object _key;
private final int _hashCode;
CacheKey(String guid, int guidHash, Object key)
{
_guid = guid;
if (key == null)
key = NULL_OBJECT;
_key = key;
_hashCode = 65521 * (17 + guidHash) + key.hashCode();
}
@Override
public final int hashCode()
{
return _hashCode;
}
@Override
public boolean equals(Object o)
{
CacheKey key = (CacheKey) o;
if (! key._key.equals(_key)) {
return false;
}
return key._guid.equals(_guid);
}
}
static class KeyHashStream extends OutputStream {
private MessageDigest _digest;
KeyHashStream(MessageDigest digest)
{
_digest = digest;
}
void init()
{
_digest.reset();
}
@Override
public void write(int value)
{
_digest.update((byte) value);
}
@Override
public void write(byte []buffer, int offset, int length)
{
_digest.update(buffer, offset, length);
}
public byte []digest()
{
byte []digest = _digest.digest();
return digest;
}
@Override
public void flush()
{
}
@Override
public void close()
{
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy