19#ifndef INCLUDED_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
20#define INCLUDED_STOC_SOURCE_COREREFLECTION_LRUCACHE_HXX
25#include <rtl/ustring.hxx>
29#include <unordered_map>
36template<
class t_Key,
class t_Val,
class t_KeyHash >
46 typedef std::unordered_map< t_Key, CacheEntry *, t_KeyHash >
t_Key2Element;
55 inline void toFront( CacheEntry * pEntry )
const;
70 inline t_Val
getValue(
const t_Key & rKey )
const;
76 inline void setValue(
const t_Key & rKey,
const t_Val & rValue );
83template<
class t_Key,
class t_Val,
class t_KeyHash >
85#ifdef __CACHE_DIAGNOSE
86 : _nCachedElements( 4 )
88 : _nCachedElements( 256 )
94 _pBlock.reset(
new CacheEntry[_nCachedElements]);
95 _pHead = _pBlock.get();
96 _pTail = _pBlock.get() + _nCachedElements - 1;
97 for (sal_Int32
nPos = _nCachedElements;
nPos--;)
100 _pBlock[
nPos].pSucc = _pBlock.get() +
nPos + 1;
104template<
class t_Key,
class t_Val,
class t_KeyHash >
107 if (pEntry != _pHead)
110 if (pEntry == _pTail)
112 _pTail = pEntry->
pPred;
120 _pHead->
pPred = pEntry;
121 pEntry->
pSucc = _pHead;
126template<
class t_Key,
class t_Val,
class t_KeyHash >
129 std::scoped_lock aGuard( _aCacheMutex );
130 const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
131 if (iFind != _aKey2Element.end())
135#ifdef __CACHE_DIAGNOSE
136 SAL_INFO(
"stoc.corerefl",
"> retrieved element \"" );
138 SAL_INFO(
"stoc.corerefl",
"\" from cache <" );
145template<
class t_Key,
class t_Val,
class t_KeyHash >
147 const t_Key & rKey,
const t_Val & rValue )
149 std::scoped_lock aGuard( _aCacheMutex );
150 if (_nCachedElements > 0)
152 const typename t_Key2Element::const_iterator iFind( _aKey2Element.find( rKey ) );
155 if (iFind == _aKey2Element.end())
158#ifdef __CACHE_DIAGNOSE
159 if (pEntry->
aKey.getLength())
161 SAL_INFO(
"stoc.corerefl",
"> kicking element \"" );
163 SAL_INFO(
"stoc.corerefl",
"\" from cache <" );
166 _aKey2Element.erase( pEntry->
aKey );
168 _aKey2Element[ rKey ] = pEntry;
172 pEntry = (*iFind).second;
173#ifdef __CACHE_DIAGNOSE
174 SAL_INFO(
"stoc.corerefl",
"> replacing element \"" );
176 SAL_INFO(
"stoc.corerefl",
"\" in cache <" );
179 pEntry->
aVal = rValue;
184template<
class t_Key,
class t_Val,
class t_KeyHash >
187 std::scoped_lock aGuard( _aCacheMutex );
188 _aKey2Element.clear();
189 for ( sal_Int32
nPos = _nCachedElements;
nPos--; )
191 _pBlock[
nPos].aKey = t_Key();
192 _pBlock[
nPos].aVal = t_Val();
194 _nCachedElements = 0;
195#ifdef __CACHE_DIAGNOSE
196 SAL_INFO(
"stoc.corerefl",
"> cleared cache <" );
Implementation of a least recently used (lru) cache.
void toFront(CacheEntry *pEntry) const
t_Key2Element _aKey2Element
sal_Int32 _nCachedElements
void clear()
Clears the cache, thus releasing all cached elements and keys.
void setValue(const t_Key &rKey, const t_Val &rValue)
Sets a value to be cached for given key.
t_Val getValue(const t_Key &rKey) const
Retrieves a value from the cache.
std::unique_ptr< CacheEntry[]> _pBlock
std::unordered_map< t_Key, CacheEntry *, t_KeyHash > t_Key2Element
#define SAL_INFO(area, stream)
LRU_Cache< OUString, css::uno::Any, OUStringHash > LRU_CacheAnyByOUString
Template instance for OUString keys, Any values.