yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#pragma once 2#include "../core/slang-crypto.h" 3#include "../core/slang-io.h" 4#include "../core/slang-string.h" 5#include "slang.h" 6 7#include <mutex> 8 9namespace Slang 10{ 11 12/// Implements a simple persistent cache on the filesystem for storing key/value pairs. 13/// Keys are SHA1 hashes and values are arbitrary blobs of data. 14/// The cache is save for concurrent access from multiple threads/processes by using 15/// a lock file within the cache directory. Furthermore, the cache implements a LRU 16/// eviction policy. 17class PersistentCache :public RefObject 18{ 19public : 20struct Desc 21 { 22// The root directory for the cache. 23const char * directory = nullptr; 24// The maximum number of entries stored in the cache. By default, there is no limit. 25Count maxEntryCount = 0 ; 26 }; 27 28struct Stats 29{ 30// Number of cache hits since last resetting the stats. 31Count hitCount ; 32// Number of cache misses since last resetting the stats. 33Count missCount ; 34// Current number of entries in the cache. 35Count entryCount ; 36}; 37 38using Key = SHA1 ::Digest; 39 40PersistentCache( const Desc & desc); 41~ PersistentCache (); 42 43/// Clear the contents of the cache by removing the cache index and all entry files. 44SlangResult clear (); 45 46const Stats & getStats () const { return m_stats; } 47void resetStats (); 48 49/// Read an entry from the cache. 50/// Returns SLANG_OK if successful, SLANG_E_NOT_FOUND if the entry is not in the cache. 51SlangResult readEntry ( const Key & key, ISlangBlob ** outData); 52 53/// Write an entry to the cache. 54/// Returns SLANG_OK if successful. 55SlangResult writeEntry ( const Key & key, ISlangBlob * data); 56 57private : 58struct CacheEntry 59{ 60Key key ; 61uint32_t age ; 62}; 63 64using CacheIndex = List < CacheEntry > ; 65 66SlangResult initialize (); 67 68String getEntryFileName ( const Key & key); 69 70SlangResult readIndex ( const String & fileName, CacheIndex & outIndex); 71SlangResult writeIndex ( const String & fileName, const CacheIndex & index); 72 73String m_cacheDirectory; 74String m_lockFileName; 75String m_indexFileName; 76 77// For exclusive locking we need both a mutex (acquired first) 78// followed by a a file lock. The mutex is needed because on Linux 79// the file lock is only locking between processes, not threads. 80std :: mutex m_mutex; 81Slang :: LockFile m_lockFile; 82 83Count m_maxEntryCount; 84 85Stats m_stats; 86 87// Used for unit tests. 88friend struct PersistentCacheTest; 89}; 90 91} // namespace Slang