# uvm_cache(KEY_T,DATA_T)

Abstract base class for implementing a key/value pair cache.

A key/value cache is similar in nature to an associative array, however there are certain conditions under which a cache may choose to evict an element which has been previously stored. The eviction policy is specific to the implementation of the cache.

@uvm-contrib

Summary

uvm_cache(KEY_T,DATA_T)
Abstract base class for implementing a key/value pair cache.
Class Hierarchy
uvm_object
uvm_cache(KEY_T,DATA_T)
Class Declaration
> virtual class uvm_cache#(
> | type | KEY_T | = | int,
> | type | DATA_T | = | int
> ) extends uvm_object
Types |
this_type | Typedef representing this cache parameterization.
optional_data | The cache can be queried for data at a given key.
optional_keys | The optional_keys type is similar to the optional_data type, however it stores keys instead of data.
size_t | An unsigned data type used for size operations on the cache.
Methods |
new | Constructor, initializes the cache.
set_max_size | Sets the maximum size of the cache to max_size.
get_max_size | Returns the current maximum size of the cache.
size | Returns the current number of elements in the cache.
exists | Returns true if key exists in the cache, otherwise returns false.
get | Returns data associated with key.
put | Puts data in cache at index key.
evict | Removes the data at key from the cache.
evict_to_max | Hook for evicting all keys greater than maximum size.
keys | Returns a queue of all keys currently in the cache.
flush | Evicts all keys from the cache.

### Types

### this_type

Typedef representing this cache parameterization.

### optional_data

The cache can be queried for data at a given key. If the key exists within the cache then the data can be returned, but if the key does not exist then the cache needs to return “nothing”.

The optional_data type is a simple wrapper around a queue of DATA_T. If the key is found (ie. hit), then the first (and only) element of the queue stores the value. If the key is not found (ie. missed), then an empty queue is returned.

Note: SystemVerilog uses a similar pattern for the array locator methods.

### optional_keys

The optional_keys type is similar to the optional_data type, however it stores keys instead of data. This is generally used by the keys method.

### size_t

An unsigned data type used for size operations on the cache.

### Methods

### new

> function new(
> | string | name | = | "unnamed-uvm_cache",
> | size_t | max_size | = | 256
> )

Constructor, initializes the cache.

The max_size argument defines the initial maximum size of the cache. The default max_size shall be 256.

A max_size of 0 indicates that the cache is unsized, and will not evict any keys.

### set_max_size

> virtual function void set_max_size(
> | size_t | max_size | = | 256
> )

Sets the maximum size of the cache to max_size.

The default value of max_size shall be 256.

If the new max_size value is less than the current number of keys stored in the cache, then evict_to_max is automatically called.

A max_size of 0 indicates that the cache is unsized, and will not evict any keys.

### get_max_size

> virtual function size_t get_max_size()

Returns the current maximum size of the cache.

### size

> pure virtual function size_t size()

Returns the current number of elements in the cache.

### exists

> pure virtual function bit exists(
> | KEY_T | key
> )

Returns true if key exists in the cache, otherwise returns false.

### get

> pure virtual function optional_data get(
> | KEY_T | key
> )

Returns data associated with key.

If key exists within the cache, then the data is returned via optional_data.

If key does not existing within the cache, then this operation shall have no effect on the cache, and shall return empty optional_data.

### put

> pure virtual function void put(
> | KEY_T | key,
> | DATA_T | data
> )

Puts data in cache at index key.

If key exists within the cache, then the data associated key is updated. If key does not exists within the cache, then a new data value is added at key.

If putting the key in the cache causes the number of stored keys to exceed get_max_size, then the least recently used key shall be evicted via evict.

### evict

> pure virtual function optional_data evict(
> | KEY_T | key
> )

Removes the data at key from the cache.

If key exists within the cache, then the data is returned via optional_data.

If key does not exist within the cache, then this operation shall have no effect on the cache, and shall return empty optional data.

### evict_to_max

> pure virtual protected function void evict_to_max()

Hook for evicting all keys greater than maximum size.

This method is called by set_max_size when the new maximum size exceeds the current size of the cache.

If the current size of the cache is less than the maximium size, evict_to_max shall have no effect on the cache.

### keys

> pure virtual function optional_keys keys()

Returns a queue of all keys currently in the cache.

### flush

> virtual function void flush()

Evicts all keys from the cache.

> virtual class uvm_cache#(
> | type | KEY_T | = | int,
> | type | DATA_T | = | int
> ) extends uvm_object

Abstract base class for implementing a key/value pair cache.
The cache can be queried for data at a given key.

> function new(
> | string | name | = | "unnamed-uvm_cache",
> | size_t | max_size | = | 256
> )

Constructor, initializes the cache.

> virtual function void set_max_size(
> | size_t | max_size | = | 256
> )

Sets the maximum size of the cache to max_size.

> virtual function size_t get_max_size()

Returns the current maximum size of the cache.

> pure virtual function size_t size()

Returns the current number of elements in the cache.

> pure virtual function bit exists(
> | KEY_T | key
> )

Returns true if key exists in the cache, otherwise returns false.

> pure virtual function optional_data get(
> | KEY_T | key
> )

Returns data associated with key.

> pure virtual function void put(
> | KEY_T | key,
> | DATA_T | data
> )

Puts data in cache at index key.

> pure virtual function optional_data evict(
> | KEY_T | key
> )

Removes the data at key from the cache.

> pure virtual protected function void evict_to_max()

Hook for evicting all keys greater than maximum size.

> pure virtual function optional_keys keys()

Returns a queue of all keys currently in the cache.

> virtual function void flush()

Evicts all keys from the cache.