com.alachisoft.ncache.client.CacheItem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.client;
// Copyright (c) 2020 Alachisoft
//
// 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
import Alachisoft.NCache.Common.BitSet;
import Alachisoft.NCache.Common.Enum.UserObjectType;
import Alachisoft.NCache.Common.Extensibility.ModuleInfo;
import com.alachisoft.ncache.client.internal.caching.*;
import com.alachisoft.ncache.common.caching.EntryType;
import com.alachisoft.ncache.runtime.CacheItemPriority;
import com.alachisoft.ncache.runtime.caching.expiration.Expiration;
import com.alachisoft.ncache.runtime.dependencies.CacheDependency;
import com.alachisoft.ncache.runtime.events.EventDataFilter;
import com.alachisoft.ncache.runtime.events.EventType;
import com.alachisoft.ncache.runtime.util.NCDateTime;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import java.util.EnumSet;
import java.util.List;
/**
* NCache uses a "key" and "value" structure for storing objects in cache.
* When an object is added in cache it is stored as value and metadata against the specified key.
* This combination of value and metadata is defined as CacheItem in NCache.
* The value of the objects stored in the cache can range from being simple string types to complex objects.
*
* CacheItem class in NCache has properties that enable you to set metadata for the item to be added in cache
* in an organized way. In scenarios where multiple attributes have to be set while adding an item in cache
* using CacheItem is preferred.Using CacheItem class object removes the problem of using multiple API overloads
* on adding/updating data in cache.You can easily use the basic API overload and add/update data easily using CacheItem.
*
*
*
* You can create an instance of CacheItem class and it to the
*
* ICache cache = CacheManager.GetCache("myCache");
*
* object someData = new object();
* CacheItem item = new CacheItem(someData);
* item.Expiration = new Expiration(ExpirationType.Sliding,TimeSpan.FromMinutes(5));
* item.Priority = CacheItemPriority.High;
*
* cache.Add("someData", item);
*
*
*/
public class CacheItem implements Cloneable {
///#region Fields
private Object _value;
private Object _currentValue;
// private java.util.Date _absoluteTime = NCDateTime.MaxValue;
private Expiration _expiration = new Expiration();
///#endregion
private CacheItemPriority priority;
private CacheItemVersion version;
///#endregion
///#region Public Properties
private java.util.Date creationTime = null;
private java.util.Date lastModifiedTime = null;
private String group;
private CacheSyncDependency syncDependency;
private CacheDependency dependency;
private int size;
private BitSet flagMap;
private java.util.HashMap queryInfo;
private CacheDataModificationListener cacheItemRemovedListener;
private CacheDataModificationListener cacheItemUpdatedListener;
private EventDataFilter itemUpdatedDataFilter;
private EventDataFilter itemRemovedDataFilter;
private String typeName;
private CacheImpl cacheInstance;
private EntryType entryType;
private boolean isModuleData;
private ModuleInfo moduleInfo;
///#region Constructor
public CacheItem() {
setCacheItemPriority(CacheItemPriority.Default);
}
/**
* Initialize new instance of cache item.
*
* @param value Actual object to be stored in cache.
*/
public CacheItem(Object value) {
_value = value;
setCacheItemPriority(CacheItemPriority.Default);
}
//#endregion
///#region Properties
public final Expiration getExpiration() {
return _expiration;
}
public final void setExpiration(Expiration value) {
_expiration = value;
}
public final CacheItemPriority getCacheItemPriority() {
return priority;
}
public final void setCacheItemPriority(CacheItemPriority value) {
priority = value;
}
public final CacheItemVersion getCacheItemVersion() {
return version;
}
public final void setCacheItemVersion(CacheItemVersion value) {
version = value;
}
public final java.util.Date getCreationTime() {
return creationTime;
}
public final java.util.Date getLastModifiedTime() {
return lastModifiedTime;
}
public final String getGroup() {
return group;
}
public final void setGroup(String value) {
group = value;
}
public final CacheSyncDependency getSyncDependency() {
return syncDependency;
}
public final void setSyncDependency(CacheSyncDependency value) {
syncDependency = value;
}
public final CacheDependency getDependency() {
return dependency;
}
public final void setDependency(CacheDependency value) {
dependency = value;
}
///#endregion
///#region Internal Properties
void setLastModifiedTime(java.util.Date value) {
lastModifiedTime = value;
}
void setCreationTime(java.util.Date value) {
creationTime = value;
}
public BitSet getFlagMapInternal() { return flagMap;}
void setFlagMapInternal(BitSet value) {
flagMap = value;
}
java.util.HashMap getQueryInfoInternal() {
return queryInfo;
}
void setQueryInfo(java.util.HashMap value) {
queryInfo = value;
}
CacheDataModificationListener getCacheItemRemovedListener() {
return cacheItemRemovedListener;
}
void setCacheItemRemovedListener(CacheDataModificationListener value) {
cacheItemRemovedListener = value;
}
CacheDataModificationListener getCacheItemUpdatedListener() {
return cacheItemUpdatedListener;
}
void setCacheItemUpdatedListener(CacheDataModificationListener value) {
cacheItemUpdatedListener = value;
}
EventDataFilter getItemUpdatedDataFilter() {
return itemUpdatedDataFilter;
}
void setItemUpdatedDataFilter(EventDataFilter value) {
itemUpdatedDataFilter = value;
}
EventDataFilter getItemRemovedDataFilter() {
return itemRemovedDataFilter;
}
void setItemRemovedDataFilter(EventDataFilter value) {
itemRemovedDataFilter = value;
}
String getTypeName() {
return typeName;
}
public void setTypeNameInternal(String value) {
typeName = value;
}
CacheImpl getCacheInstance() {
return cacheInstance;
}
void setCacheInstance(CacheImpl value) {
cacheInstance = value;
}
EntryType getEntryType() {
return entryType;
}
void setEntryType(EntryType value) {
entryType = value;
}
boolean getIsModuleData() {
return isModuleData;
}
void setIsModuleData(boolean value) {
isModuleData = value;
}
ModuleInfo getModuleInfo() {
return moduleInfo;
}
void setModuleInfo(ModuleInfo value) {
moduleInfo = value;
}
///#endregion
///#region Public Methods
public final void addCacheDataNotificationListener(CacheDataModificationListener listener, EnumSet eventType, EventDataFilter datafilter) {
if (listener == null) {
return;
}
if ((eventType.contains(EventType.ItemRemoved))) {
setCacheItemRemovedListener(listener);
setItemRemovedDataFilter(datafilter);
}
if (eventType.contains(EventType.ItemUpdated)) {
setCacheItemUpdatedListener(listener);
setItemUpdatedDataFilter(datafilter);
}
}
public void removeCacheDataNotificationListener(CacheDataModificationListener listener, EnumSet eventEnumSet)
{
if( listener == null ) return;
if(eventEnumSet.contains(EventType.ItemRemoved))
{
setCacheItemRemovedListener(listener);
setItemRemovedDataFilter(EventDataFilter.None);
}
if(eventEnumSet.contains(EventType.ItemUpdated))
{
setCacheItemUpdatedListener(listener);
setItemUpdatedDataFilter(EventDataFilter.None);
}
}
public final T getValue(Class> cls) throws OperationFailedException {
if (_value instanceof ValueEmissary) {
T instanceofT = null;
if (_currentValue != null)
try {
return (T) _currentValue;
} catch (ClassCastException e) {
//ignore
}
ValueEmissary emissary = (ValueEmissary)((_value instanceof ValueEmissary) ? _value : null);
if (cacheInstance == null)
{
return (T)emissary.getData();
}
switch (emissary.getType())
{
case CacheItem:
_currentValue = cacheInstance.safeDeserialize(emissary.getData(), cacheInstance.getSerializationContext(), flagMap, UserObjectType.CacheItem, cls);
break;
}
return (T)_currentValue;
}
return (T)_value;
}
public void setValue(Object value) {
_value = value;
}
///#endregion
///#region Cloneable Members
/**
* Creates a shallow copy of CacheItem
*
* @return
*/
public final Object clone() {
CacheItem newItem = new CacheItem();
newItem.setCacheItemRemovedListener(getCacheItemRemovedListener());
newItem.setCacheItemUpdatedListener(getCacheItemUpdatedListener());
newItem.setCreationTime(getCreationTime());
newItem.setDependency(getDependency());
newItem.setExpiration(getExpiration());
newItem.flagMap = flagMap;
newItem.setGroup(getGroup());
newItem.setItemRemovedDataFilter(getItemRemovedDataFilter());
newItem.setItemUpdatedDataFilter(getItemUpdatedDataFilter());
newItem.setTypeNameInternal(getTypeName());
newItem.setLastModifiedTime(getLastModifiedTime());
newItem.setCacheItemPriority(getCacheItemPriority());
newItem.queryInfo = queryInfo;
newItem.size= size;
newItem.setSyncDependency(getSyncDependency());
newItem._value = _value;
newItem.setCacheItemVersion(getCacheItemVersion());
newItem.setCacheInstance(getCacheInstance());
newItem.setIsModuleData(getIsModuleData());
return newItem;
}
///#endregion
}