# Enhancing Energy Efficiency in IoT-NDN via Parameter Optimization
## 5 Implementation
### Implementation of Key Parameters
#### 5.3.2 Caching Strategies  
ndnSIM already provides support for two of the presented _aching strategies_: LRU and FIFO [36]. The other strategies all had to be implemented for this framework. Every caching strategy inherits from ndnSIM's superclass Policy, which stores the policy name and a pointer to the Content Store. All of its subclasses have to implement the five virtual methods doAfterInsert, doAfterRefresh, doBeforeErase, doBeforeUse, and evictEntries. Each of these methods except evictEntries receives a reference to a CS entry as an argument. These methods are presented in [37] and outlined here:  
* doAfterInsert is called after an entry is added to the CS, but the method can decide to evict the new entry again (i.e., the caching scheme decides not to cache the element).
* doAfterRefresh is invoked when an existing entry is supposed to be refreshed as if it was newly inserted into the CS.
* doBeforeErase is invoked when an entry is \"erased due to management command,\" not for the eviction of an element.
* doBeforeUse is called when an Interest's packet name matches an element in the CS.
* evictEntries may evict any number of elements but must ensure that at the end of the method, the number of CS elements does not exceed the predefined limit. Most policies will evict exactly one element per method call.  
The policies LFU, LRFU, FWF, and Random are newly implemented subclasses of Policy, and their data structures are sketched in the following.  
The key data structure of LfwPolicy is a priority queue from Boost's binomial heap [38]. This queue contains pairs of strings (representing the packets' names) and integers (representing their corresponding frequency).  
LruPolicy, like LfuPolicy, maintains a priority queue. However, in this class, the queue stores pairs of packet names and CRF values (as double). For simplicity, the base and the lambda function of the exponential weighing function are constant values, where the base is set to \(\frac{1}{2}\) and lambda to \(\frac{1}{4}\) by default. m_currentAccessTime counts how many cache requests have been made to elements and thus works like a timestamp. After each access of an element, whether it was a hit or a miss, the counter is incremented by 1. m_nameToLastAccess maps the names to their last access time, as measured by m_currentAccessTime.  
FwPolicy maintains no extraneous data structures apart from m_elementMap, which maps packet names to their corresponding CS entry reference. Unlike the other policies, evictEntries does not just evict one element but all of them.  
RandomPolicy has a random number generator m_generator. When the CS is full, it uniformly generates a random number between 0 and CS size \(-1\). To allow for random access to the elements, a std::vector is used to refer to the CS elements. In order to delete an element at index \(i\), the element is not actually deleted from the vector. Instead, the index is added to a stack of invalid indices m_invalidStack. This stack is initialized with all indices \(0,1,\dots,\text{CS size}-1\) because before any network communication, the CS is empty, rendering all CS entries invalid. As long as elements are present in the stack, a newly arriving element is assigned to the top index of m_invalidStack, which is then popped. If the stack is empty,that means the CS is full, and one of the old entries has to be evicted when a new packet arrives at the CS. Here, m_generator is used to get a random index to evict.