mirror of https://github.com/OpenMW/openmw.git
Use a common base class for resource managers
Implement updateCache to delete unreferenced cached objects when they have not been referenced for a while.pull/893/head
parent
ea1efaac0c
commit
df57d4bfba
@ -0,0 +1,34 @@
|
||||
#include "resourcemanager.hpp"
|
||||
|
||||
#include "objectcache.hpp"
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
ResourceManager::ResourceManager(const VFS::Manager *vfs, const double expiryDelay)
|
||||
: mVFS(vfs)
|
||||
, mCache(new osgDB::ObjectCache)
|
||||
, mExpiryDelay(expiryDelay)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ResourceManager::updateCache(double referenceTime)
|
||||
{
|
||||
// NOTE: we could clear the cache from the background thread if the deletion proves too much of an overhead
|
||||
// idea: customize objectCache to not hold a lock while doing the actual deletion
|
||||
mCache->updateTimeStampOfObjectsInCacheWithExternalReferences(referenceTime);
|
||||
mCache->removeExpiredObjectsInCache(referenceTime - mExpiryDelay);
|
||||
}
|
||||
|
||||
void ResourceManager::clearCache()
|
||||
{
|
||||
mCache->clear();
|
||||
}
|
||||
|
||||
const VFS::Manager* ResourceManager::getVFS() const
|
||||
{
|
||||
return mVFS;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
#ifndef OPENMW_COMPONENTS_RESOURCE_MANAGER_H
|
||||
#define OPENMW_COMPONENTS_RESOURCE_MANAGER_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
class Manager;
|
||||
}
|
||||
|
||||
namespace osgDB
|
||||
{
|
||||
class ObjectCache;
|
||||
}
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
/// @brief Base class for managers that require a virtual file system and object cache.
|
||||
/// @par This base class implements clearing of the cache, but populating it and what it's used for is up to the individual sub classes.
|
||||
class ResourceManager
|
||||
{
|
||||
public:
|
||||
/// @param expiryDelay how long to keep objects in cache after no longer being referenced.
|
||||
ResourceManager(const VFS::Manager* vfs, const double expiryDelay = 300.0);
|
||||
|
||||
/// Clear cache entries that have not been referenced for longer than expiryDelay.
|
||||
virtual void updateCache(double referenceTime);
|
||||
|
||||
/// Clear all cache entries regardless of having external references.
|
||||
virtual void clearCache();
|
||||
|
||||
const VFS::Manager* getVFS() const;
|
||||
|
||||
protected:
|
||||
const VFS::Manager* mVFS;
|
||||
osg::ref_ptr<osgDB::ObjectCache> mCache;
|
||||
double mExpiryDelay;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue