public final class GemfireCache implements Cache {
private static Region<Object, Object> mybatis_region = null;
private Region<Object, Object> region = null;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
public void setId(String id) {
this.id = id;
}
public void setRegion(Region<Object, Object> region) {
this.region = region;
}
public Region<Object, Object> getRegion() {
return region;
}
@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
public GemfireCache(String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
region = getParentRegion().getSubregion(id);
if (null == region) {
AttributesFactory attrFactory =
new AttributesFactory(mybatis_region.getAttributes());
region = mybatis_region.createSubregion(id, attrFactory.create());
}
}
public void clear() {
if (null != region) {
try {
region.clear();
} catch (Throwable t) {
throw new CacheException(t);
}
}
}
public String getId() {
return this.id;
}
public Object getObject(Object key) {
Object retVal = null;
if (null != region) {
if (region.containsKey(key.hashCode())) {
try {
retVal = region.get(key.hashCode());
} catch (Throwable t) {
throw new CacheException(t);
}
}
}
return retVal;
}
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
public int getSize() {
if (null != region) {
try {
return region.size();
} catch (Throwable t) {
throw new CacheException(t);
}
}
return 0;
}
public void putObject(Object key, Object value) {
if (null != region) {
try {
region.put(key.hashCode(), value);
} catch (Throwable t) {
throw new CacheException(t);
}
}
}
public Object removeObject(Object key) {
if (null != region) {
try {
return region.remove(key.hashCode());
} catch (Throwable t) {
throw new CacheException(t);
}
}
return null;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Cache)) {
return false;
}