You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by hc...@apache.org on 2005/01/22 22:34:09 UTC
cvs commit: jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core CacheEntry.java CacheManager.java CacheManagerUtils.java ICache.java ICacheSafe.java SafeCacheManager.java
hchar 2005/01/22 13:34:09
Added: sandbox/yajcache/src/org/apache/jcs/yajcache/core
CacheEntry.java CacheManager.java
CacheManagerUtils.java ICache.java ICacheSafe.java
SafeCacheManager.java
Log:
move yajcache to sandbox
Revision Changes Path
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/CacheEntry.java
Index: CacheEntry.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import java.util.Map;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.jcs.yajcache.annotate.*;
/**
*
* @author Hanson Char
*/
@CopyRightApache
public class CacheEntry<V> implements Map.Entry<String,V> {
private final String key;
private V value;
/** Creates a new instance of CacheEntry */
public CacheEntry(String key, V val) {
this.key = key;
this.value = val;
}
public String getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V val) {
V ret = this.value;
this.value = val;
return ret;
}
@Override public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/CacheManager.java
Index: CacheManager.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.jcs.yajcache.soft.SoftRefCache;
import org.apache.jcs.yajcache.annotate.*;
/**
* @author Hanson Char
*/
@CopyRightApache
public enum CacheManager {
inst;
// Cache name to Cache mapping.
private final ConcurrentMap<String,ICache<?>> map =
new ConcurrentHashMap<String, ICache<?>>();
/**
* Returns the cache for the specified name and value type;
* Creates the cache if necessary.
*
* @throws ClassCastException if the cache already exists for an
* incompatible value type.
*/
// @SuppressWarnings({"unchecked"})
public <V> ICache<V> getCache(String name, Class<V> valueType)
{
ICache c = this.map.get(name);
if (c == null)
c = this.createCache(name, valueType);
else
CacheManagerUtils.inst.checkValueType(c, valueType);
return c;
}
/**
* Returns an existing cache for the specified name; or null if not found.
*/
public ICache getCache(String name) {
return this.map.get(name);
}
/**
* Removes the specified cache, if it exists.
*/
public ICache removeCache(String name) {
ICache c = this.map.remove(name);
if (c != null) {
c.clear();
}
return c;
}
/**
* Creates the specified cache if not already created.
*
* @return either the cache created by the current thread, or
* an existing cache created earlier by another thread.
*/
// @SuppressWarnings({"unchecked"})
private <V> ICache<V> createCache(String name, Class<V> valueType) {
ICache<V> c = new SoftRefCache<V>(name, valueType);
ICache old = this.map.putIfAbsent(name, c);
if (old != null) {
// race condition: cache already created by another thread.
CacheManagerUtils.inst.checkValueType(old, valueType);
c = old;
}
return c;
}
@TestOnly("Used solely to simluate a race condition during cache creation ")
<V> ICache<V> testCreateCacheRaceCondition(String name, Class<V> valueType)
{
return this.createCache(name, valueType);
}
}
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/CacheManagerUtils.java
Index: CacheManagerUtils.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import org.apache.jcs.yajcache.annotate.*;
/**
*
* @author Hanson Char
*/
@CopyRightApache
enum CacheManagerUtils {
inst;
/** Checks the value type assignability of an existing cache. */
void checkValueType(ICache c, Class<?> valueType) {
Class<?> cacheValueType = c.getValueType();
if (!cacheValueType.isAssignableFrom(valueType))
throw new ClassCastException("Cache " + c.getName()
+ " of " + c.getValueType()
+ " already exists and cannot be used for " + valueType);
return;
}
}
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/ICache.java
Index: ICache.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import java.util.Map;
import org.apache.jcs.yajcache.annotate.*;
/**
* Interface of a Cache.
*
* @author Hanson Char
*/
@CopyRightApache
@ThreadSafety(ThreadSafetyType.SAFE)
public interface ICache<V> extends Map<String,V> {
/** Returns the cache name. */
@ThreadSafety(ThreadSafetyType.IMMUTABLE)
public String getName();
/** Returns the value type of the cached items. */
@ThreadSafety(ThreadSafetyType.IMMUTABLE)
public Class<V> getValueType();
/** Returns the value cached for the specified key. */
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="This method itself is thread-safe. However,"
+ " the thread-safetyness of the return value cannot be guaranteed"
+ " by this interface."
)
public V get(String key);
}
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/ICacheSafe.java
Index: ICacheSafe.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import java.util.Map;
import org.apache.jcs.yajcache.annotate.*;
/**
*
* @author Hanson Char
*/
@CopyRightApache
@ThreadSafety(ThreadSafetyType.SAFE)
public interface ICacheSafe<V> extends ICache<V> {
/**
* If the cache value is Serializable, returns a deep clone copy of
* the cached value. Else, the behavior is the same as get.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
note="The return value is guaranteed to be thread-safe"
+ " only if the return value type is Serializable."
)
public V getCopy(String key);
/**
* If the cache value is Serializable, puts a deep clone copy of
* the given value to the cache. Else, the behavior is the same as put.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
note="The given value is guaranteed to be thread-safe"
+ " only if the value type is Serializable."
)
public V putCopy(String key, V value);
/**
* If the cache value is Serializable, puts the deep clone copies of
* the given values to the cache. Else, the behavior is the same as putAll.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="The thread-safetyness of the given map cannot be guaranteed.",
note="The given values in the map is guaranteed to be thread-safe"
+ " only if the value type is Serializable."
)
public void putAllCopies(Map<? extends String, ? extends V> map);
/**
* Treats the cache value as a Java Bean and returns a deep clone copy of
* the cached value.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
note="The return value is guaranteed to be thread-safe"
+ " only if the return value is a Java Bean."
)
public V getBeanCopy(String key);
/**
* Treats the cache value as a Java Bean and puts a deep clone copy of
* the given value to the cache.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
note="The given value is guaranteed to be thread-safe"
+ " only if the value is a Java Bean."
)
public V putBeanCopy(String key, V value);
/**
* Treats the cache value as a Java Bean and puts the deep clone copies of
* the given values to the cache.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="The thread-safetyness of the given map cannot be guaranteed"
+ " by this interface.",
note="The given values in the map is guaranteed to be thread-safe"
+ " only if the values are Java Beans."
)
public void putAllBeanCopies(Map<? extends String, ? extends V> map);
/**
* Treats the cache value as a Java Bean and returns a shallow clone copy of
* the cached value.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="The thread-safetyness of the members of the return value cannot"
+ " be garanteed by this interface.",
note="The return value is guaranteed to be thread-safe"
+ " only if the return value is a JavaBean."
)
public V getBeanClone(String key);
/**
* Treats the cache value as a Java Bean and puts a shallow clone copy of
* the given value to the cache.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="The thread-safetyness of the members of the given value cannot"
+ " be garanteed by this interface.",
note="The given value is guaranteed to be thread-safe"
+ " only if the value is a Java Bean."
)
public V putBeanClone(String key, V value);
/**
* Treats the cache value as a Java Bean and puts the shallow clone copies of
* the given values to the cache.
*/
@ThreadSafety(
value=ThreadSafetyType.SAFE,
caveat="The thread-safetyness of the given map cannot be guaranteed"
+ " by this interface."
+ " Also, the thread-safetyness of the members of each value"
+ " in the map cannot be garanteed.",
note="The given values in the map is guaranteed to be thread-safe"
+ " only if the values are Java Beans."
)
public void putAllBeanClones(Map<? extends String, ? extends V> map);
}
1.1 jakarta-turbine-jcs/sandbox/yajcache/src/org/apache/jcs/yajcache/core/SafeCacheManager.java
Index: SafeCacheManager.java
===================================================================
/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jcs.yajcache.core;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.jcs.yajcache.soft.SoftRefCacheSafe;
import org.apache.jcs.yajcache.annotate.*;
/**
* @author Hanson Char
*/
@CopyRightApache
public enum SafeCacheManager {
inst;
// Cache name to Cache mapping.
private final ConcurrentMap<String,ICacheSafe> map =
new ConcurrentHashMap<String, ICacheSafe>();
/**
* Returns the cache for the specified name and value type;
* Creates the cache if necessary.
*
* @throws ClassCastException if the cache already exists for an
* incompatible value type.
*/
public <V> ICacheSafe<V> getCache(String name, Class<V> valueType)
{
ICacheSafe c = this.map.get(name);
if (c == null)
c = this.createCache(name, valueType);
else
CacheManagerUtils.inst.checkValueType(c, valueType);
return c;
}
/**
* Returns an existing cache for the specified name; or null if not found.
*/
public ICacheSafe getCache(String name) {
return this.map.get(name);
}
/**
* Removes the specified cache, if it exists.
*/
public ICacheSafe removeCache(String name) {
ICacheSafe c = this.map.remove(name);
if (c != null) {
c.clear();
}
return c;
}
/**
* Creates the specified cache if not already created.
*
* @return either the cache created by the current thread, or
* an existing cache created earlier by another thread.
*/
private <V> ICacheSafe<V> createCache(String name, Class<V> valueType) {
ICacheSafe c = new SoftRefCacheSafe<V>(name, valueType);
ICacheSafe old = this.map.putIfAbsent(name, c);
if (old != null) {
// race condition: cache already created by another thread.
CacheManagerUtils.inst.checkValueType(old, valueType);
c = old;
}
return c;
}
@TestOnly("Used soley to simluate a race condition during cache creation")
<V> ICacheSafe<V> testCreateCacheRaceCondition(
String name, Class<V> valueType)
{
return this.createCache(name, valueType);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org