Show / Hide Table of Contents

Class AbstractKeyBasedCache

AbstractKeyBasedCache is a base class for ICache implementations.

Inheritance
object
AbstractKeyBasedCache
AbstractKeySetBasedCache
Implements
ICache
IDictionary
ICollection
IEnumerable
Inherited Members
object.Equals(object, object)
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
Namespace: Tangosol.Net.Cache.Support
Assembly: Coherence.dll
Syntax
public abstract class AbstractKeyBasedCache : ICache, IDictionary, ICollection, IEnumerable
Remarks

AbstractKeyBasedCache requires a read-only sub-type to implement only Get(object) and GetKeysEnumerator() methods, and a read-write sub-type to additionally implement only Insert(object, object, long) and Remove(object).

A number of the methods have implementations provided, but are extremely inefficient for caches that contain large amounts of data, including Clear(), Contains(object) and Count. Furthermore, if any of a number of method implementations has any cost of returning an "old value", then the InsertAll(IDictionary) and RemoveBlind(object) methods should also be implemented.

Properties

Count

Returns the number of key-value mappings in this cache.

Declaration
public virtual int Count { get; }
Property Value
Type Description
int

The number of key-value mappings in this cache.

Entries

Returns a collection of the mappings contained in this cache.

Declaration
public virtual ICollection Entries { get; }
Property Value
Type Description
ICollection

A collection of the mappings contained in this cache.

Remarks

Each element in the returned collectioin is an AbstractKeyBasedCache.Entry.

IsFixedSize

Gets a value indicating whether the IDictionary object has a fixed size.

Declaration
public virtual bool IsFixedSize { get; }
Property Value
Type Description
bool

Always false for this cache.

IsReadOnly

Gets a value indicating whether the IDictionary object is read-only.

Declaration
public virtual bool IsReadOnly { get; }
Property Value
Type Description
bool

Always false for this cache.

IsSynchronized

Gets a value indicating whether access to the ICollection is synchronized (thread safe).

Declaration
public virtual bool IsSynchronized { get; }
Property Value
Type Description
bool

Always false for this cache.

this[object]

Returns the value to which this cache maps the specified key.

Declaration
public virtual object this[object key] { get; set; }
Parameters
Type Name Description
object key

The key object.

Property Value
Type Description
object

The value to which this cache maps the specified key, or null if the cache contains no mapping for this key.

Keys

Returns a collection view of the keys contained in this cache.

Declaration
public virtual ICollection Keys { get; }
Property Value
Type Description
ICollection

A colleciton of the keys contained in this cache.

SyncRoot

Gets an object that can be used to synchronize access to the ICollection.

Declaration
public virtual object SyncRoot { get; }
Property Value
Type Description
object

An object that can be used to synchronize access to the ICollection">.

Values

Returns a collection of the values contained in this cache.

Declaration
public virtual ICollection Values { get; }
Property Value
Type Description
ICollection

A collection of the values contained in this cache.

Methods

Add(object, object)

Adds an element with the provided key and value to the IDictionary object.

Declaration
public virtual void Add(object key, object value)
Parameters
Type Name Description
object key

The object to use as the key of the element to add.

object value

The object to use as the value of the element to add.

Clear()

Clear all key/value mappings.

Declaration
public virtual void Clear()

Contains(object)

Returns true if this cache contains a mapping for the specified key.

Declaration
public virtual bool Contains(object key)
Parameters
Type Name Description
object key

Key whose mapping is searched for.

Returns
Type Description
bool

true if this cache contains a mapping for the specified key, false otherwise.

CopyTo(Array, int)

Copies the elements of the ICollection to an Array, starting at a particular index.

Declaration
public virtual void CopyTo(Array array, int index)
Parameters
Type Name Description
Array array

The one-dimensional Array that is the destination of the elements copied from ICollection.

int index

The zero-based index in array at which copying begins.

Equals(object)

Compares the specified object with this cache for equality.

Declaration
public override bool Equals(object o)
Parameters
Type Name Description
object o

Object to be compared for equality with this cache.

Returns
Type Description
bool

true if the specified object is equal to this cache.

Overrides
object.Equals(object)
Remarks

Returns true if the given object is also a cache and the two caches represent the same mappings. More formally, two caches

t1
and
t2
represent the same mappings if
t1.Keys.Equals(t2.Keys)
and for every key
k

in

t1.Keys
,
 (t1[k]==null ? t2[k]==null :
t1[k].Equals(t2[k]) 
. This ensures that the Equals method works properly across different implementations of the cache interface.

Get(object)

Returns the value for the specified key.

Declaration
protected abstract object Get(object key)
Parameters
Type Name Description
object key

Key whose value is returned.

Returns
Type Description
object

Value from the cache for the specified key.

GetAll(ICollection)

Get all the specified keys, if they are in the cache.

Declaration
public virtual IDictionary GetAll(ICollection keys)
Parameters
Type Name Description
ICollection keys

A collection of keys that may be in the named cache.

Returns
Type Description
IDictionary

An IDictionary of keys to values for the specified keys passed in keys.

Remarks

For each key that is in the cache, that key and its corresponding value will be placed in the dictionary that is returned by this method. The absence of a key in the returned dictionary indicates that it was not in the cache, which may imply (for caches that can load behind the scenes) that the requested data could not be loaded.

GetEnumerator()

Returns an ICacheEnumerator object for the ICache instance.

Declaration
public virtual ICacheEnumerator GetEnumerator()
Returns
Type Description
ICacheEnumerator

An ICacheEnumerator object for the ICache instance.

GetHashCode()

Returns the hash code value for this cache.

Declaration
public override int GetHashCode()
Returns
Type Description
int

The hash code value for this cache.

Overrides
object.GetHashCode()
Remarks

The hash code of a cache is defined to be the sum of the hash codes of each entry in the cache's Entries. This ensures that

t1.Equals(t2)
implies that
t1.GetHashCode()==t2.GetHashCode()
for any two caches
t1
and
t2
, as required by the general

contract of object.GetHashCode.

GetKeysEnumerator()

Create an IEnumerator over the keys in this cache.

Declaration
protected abstract IEnumerator GetKeysEnumerator()
Returns
Type Description
IEnumerator

A new instance of an IEnumerator over the keys in this cache.

Insert(object, object)

Associates the specified value with the specified key in this cache.

Declaration
public virtual object Insert(object key, object value)
Parameters
Type Name Description
object key

Key with which the specified value is to be associated.

object value

Value to be associated with the specified key.

Returns
Type Description
object

Previous value associated with specified key, or null if there was no mapping for key.

Insert(object, object, long)

Associates the specified value with the specified key in this cache.

Declaration
public virtual object Insert(object key, object value, long millis)
Parameters
Type Name Description
object key

Key with which the specified value is to be associated.

object value

Value to be associated with the specified key.

long millis

The number of milliseconds until the cache entry will expire, also referred to as the entry's "time to live"; pass DEFAULT to use the cache's default time-to-live setting; pass NEVER to indicate that the cache entry should never expire; this milliseconds value is not a date/time value, but the amount of time object will be kept in the cache.

Returns
Type Description
object

Previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the cache previously associated null with the specified key, if the implementation supports null values.

Remarks

If the cache previously contained a mapping for this key, the old value is replaced.

This variation of the Insert(object, object) method allows the caller to specify an expiry (or "time to live") for the cache entry.
Exceptions
Type Condition
NotSupportedException

If the requested expiry is a positive value and the implementation does not support expiry of cache entries.

InsertAll(IDictionary)

Copies all of the mappings from the specified IDictionary to this cache.

Declaration
public virtual void InsertAll(IDictionary dictionary)
Parameters
Type Name Description
IDictionary dictionary

The IDictionary containing the key/value pairings to insert into this cache.

Remarks

The effect of this call is equivalent to that of calling Insert(object, object) on this cache once for each mapping in the passed dictionary. The behavior of this operation is unspecified if the passed dictionary is modified while the operation is in progress.

InstantiateEntriesCollection()

Factory pattern: Create a collection that represents the entries in the cache.

Declaration
protected virtual ICollection InstantiateEntriesCollection()
Returns
Type Description
ICollection

A new instance of ICollection that represents the entries in the cache.

InstantiateKeysCollection()

Factory pattern: Create a collection that represents the keys in the cache.

Declaration
protected virtual ICollection InstantiateKeysCollection()
Returns
Type Description
ICollection

A new instance of ICollection that represents the keys in the cache.

InstantiateValuesCollection()

Factory pattern: Instantiate the values collection.

Declaration
protected virtual ICollection InstantiateValuesCollection()
Returns
Type Description
ICollection

A new instance of ICollection that represents this cache's values.

Remove(object)

Removes the mapping for this key from this cache if present.

Declaration
public virtual void Remove(object key)
Parameters
Type Name Description
object key

Key whose mapping is to be removed from the cache.

Remarks

Expensive: updates both the underlying cache and the local cache.

RemoveBlind(object)

Removes the mapping for this key from this cache if present.

Declaration
protected virtual bool RemoveBlind(object key)
Parameters
Type Name Description
object key

Key whose mapping is to be removed from the cache.

Returns
Type Description
bool

true iff the cache changed as the result of this operation.

Remarks

This method exists to allow sub-types to optimize remove functionalitly for situations in which the original value is not required.

ToString()

Returns a string representation of this cache.

Declaration
public override string ToString()
Returns
Type Description
string

A string representation of this cache.

Overrides
object.ToString()
Remarks

The string representation consists of a list of key-value mappings in the order returned by the cache's Entries enumerator, enclosed in braces (

"{}"
). Adjacent mappings are separated by the characters
", "
(comma and space). Each key-value mapping is rendered as the key followed by an equals sign (
"="
) followed by the associated value. Keys and values are converted to strings as by
object.ToString()
.

Implements

ICache
IDictionary
ICollection
IEnumerable
In this article
Back to top Copyright © 2000, 2024, Oracle and/or its affiliates.