Extracted image cache

This commit is contained in:
emeric
2024-03-29 21:02:36 +01:00
parent fb6670290e
commit 58df9284d4
4 changed files with 162 additions and 102 deletions
+13 -50
View File
@@ -117,7 +117,7 @@ namespace lms::cover
CoverService::CoverService(db::Db& db,
const std::filesystem::path& defaultSvgCoverPath)
: _db{ db }
, _maxCacheSize{ core::Service<core::IConfig>::get()->getULong("cover-max-cache-size", 30) * 1000 * 1000 }
, _cache{ core::Service<core::IConfig>::get()->getULong("cover-max-cache-size", 30) * 1000 * 1000 }
, _maxFileSize{ core::Service<core::IConfig>::get()->getULong("cover-max-file-size", 10) * 1000 * 1000 }
, _preferredFileNames{ constructPreferredFileNames() }
, _artistFileNames{ constructArtistFileNames() }
@@ -125,7 +125,7 @@ namespace lms::cover
setJpegQuality(core::Service<core::IConfig>::get()->getULong("cover-jpeg-quality", 75));
LMS_LOG(COVER, INFO, "Default cover path = '" << defaultSvgCoverPath.string() << "'");
LMS_LOG(COVER, INFO, "Max cache size = " << _maxCacheSize);
LMS_LOG(COVER, INFO, "Max cache size = " << _cache.getMaxCacheSize());
LMS_LOG(COVER, INFO, "Max file size = " << _maxFileSize);
LMS_LOG(COVER, INFO, "Preferred file names: " << core::stringUtils::joinStrings(_preferredFileNames, ","));
@@ -199,7 +199,7 @@ namespace lms::cover
std::unique_ptr<IEncodedImage> image;
for (std::string_view filename : preferredFileNames)
for (const std::string_view filename : preferredFileNames)
{
image = tryLoadImageFromFilename(filename);
if (image)
@@ -267,7 +267,7 @@ namespace lms::cover
std::error_code ec;
std::filesystem::directory_iterator itPath(directoryPath, ec);
std::filesystem::directory_iterator itEnd;
const std::filesystem::directory_iterator itEnd;
while (!ec && itPath != itEnd)
{
const std::filesystem::path& path{ *itPath };
@@ -306,9 +306,9 @@ namespace lms::cover
{
using namespace db;
const CacheEntryDesc cacheEntryDesc{ trackId, width };
const ImageCache::EntryDesc cacheEntryDesc{ trackId, width };
std::shared_ptr<IEncodedImage> cover{ loadFromCache(cacheEntryDesc) };
std::shared_ptr<IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
if (cover)
return cover;
@@ -331,7 +331,7 @@ namespace lms::cover
}
if (cover)
saveToCache(cacheEntryDesc, cover);
_cache.addImage(cacheEntryDesc, cover);
return cover;
}
@@ -339,9 +339,9 @@ namespace lms::cover
std::shared_ptr<IEncodedImage> CoverService::getFromRelease(db::ReleaseId releaseId, ImageSize width)
{
using namespace db;
const CacheEntryDesc cacheEntryDesc{ releaseId, width };
const ImageCache::EntryDesc cacheEntryDesc{ releaseId, width };
std::shared_ptr<IEncodedImage> cover{ loadFromCache(cacheEntryDesc) };
std::shared_ptr<IEncodedImage> cover{ _cache.getImage(cacheEntryDesc) };
if (cover)
return cover;
@@ -380,7 +380,7 @@ namespace lms::cover
}
if (cover)
saveToCache(cacheEntryDesc, cover);
_cache.addImage(cacheEntryDesc, cover);
return cover;
}
@@ -388,9 +388,9 @@ namespace lms::cover
std::shared_ptr<IEncodedImage> CoverService::getFromArtist(db::ArtistId artistId, ImageSize width)
{
using namespace db;
const CacheEntryDesc cacheEntryDesc{ artistId, width };
const ImageCache::EntryDesc cacheEntryDesc{ artistId, width };
std::shared_ptr<IEncodedImage> artistImage{ loadFromCache(cacheEntryDesc) };
std::shared_ptr<IEncodedImage> artistImage{ _cache.getImage(cacheEntryDesc) };
if (artistImage)
return artistImage;
@@ -480,20 +480,13 @@ namespace lms::cover
}
if (artistImage)
saveToCache(cacheEntryDesc, artistImage);
_cache.addImage(cacheEntryDesc, artistImage);
return artistImage;
}
void CoverService::flushCache()
{
std::unique_lock lock{ _cacheMutex };
LMS_LOG(COVER, DEBUG, "Cache stats: hits = " << _cacheHits << ", misses = " << _cacheMisses << ", nb entries = " << _cache.size() << ", size = " << _cacheSize);
_cacheHits = 0;
_cacheMisses = 0;
_cacheSize = 0;
_cache.clear();
}
void CoverService::setJpegQuality(unsigned quality)
@@ -503,35 +496,5 @@ namespace lms::cover
LMS_LOG(COVER, INFO, "JPEG export quality = " << _jpegQuality);
}
void CoverService::saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<IEncodedImage> image)
{
std::unique_lock lock{ _cacheMutex };
while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
{
auto itRandom{ core::random::pickRandom(_cache) };
_cacheSize -= itRandom->second->getDataSize();
_cache.erase(itRandom);
}
_cacheSize += image->getDataSize();
_cache[entryDesc] = image;
}
std::shared_ptr<IEncodedImage> CoverService::loadFromCache(const CacheEntryDesc& entryDesc)
{
std::shared_lock lock{ _cacheMutex };
auto it{ _cache.find(entryDesc) };
if (it == std::cend(_cache))
{
++_cacheMisses;
return nullptr;
}
++_cacheHits;
return it->second;
}
} // namespace lms::cover
+3 -52
View File
@@ -19,19 +19,14 @@
#pragma once
#include <atomic>
#include <filesystem>
#include <map>
#include <optional>
#include <shared_mutex>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>
#include "services/cover/ICoverService.hpp"
#include "image/IEncodedImage.hpp"
#include "database/Types.hpp"
#include "ImageCache.hpp"
namespace lms::db
{
@@ -43,42 +38,6 @@ namespace lms::av
class IAudioFile;
}
namespace lms::cover
{
struct CacheEntryDesc
{
std::variant<db::ArtistId, db::ReleaseId, db::TrackId> id;
std::size_t size;
bool operator==(const CacheEntryDesc& other) const
{
return id == other.id
&& size == other.size;
}
};
} // ns Cover
namespace std
{
template<>
class hash<lms::cover::CacheEntryDesc>
{
public:
size_t operator()(const lms::cover::CacheEntryDesc& e) const
{
size_t h{};
std::visit([&](auto id)
{
using IdType = std::decay_t<decltype(id)>;
h ^= std::hash<IdType>{}(id);
}, e.id);
h ^= std::hash<std::size_t>{}(e.size) << 1;
return h;
}
};
} // ns std
namespace lms::cover
{
class CoverService : public ICoverService
@@ -106,21 +65,13 @@ namespace lms::cover
std::unique_ptr<image::IEncodedImage> getFromDirectory(const std::filesystem::path& directory, image::ImageSize width, const std::vector<std::string>& preferredFileNames, bool allowPickRandom) const;
std::unique_ptr<image::IEncodedImage> getFromSameNamedFile(const std::filesystem::path& filePath, image::ImageSize width) const;
bool checkCoverFile(const std::filesystem::path& directoryPath) const;
bool checkCoverFile(const std::filesystem::path& filePath) const;
db::Db& _db;
std::shared_mutex _cacheMutex;
std::unordered_map<CacheEntryDesc, std::shared_ptr<image::IEncodedImage>> _cache;
ImageCache _cache;
std::shared_ptr<image::IEncodedImage> _defaultCover;
std::atomic<std::size_t> _cacheMisses{};
std::atomic<std::size_t> _cacheHits{};
std::size_t _cacheSize{};
void saveToCache(const CacheEntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image);
std::shared_ptr<image::IEncodedImage> loadFromCache(const CacheEntryDesc& entryDesc);
const std::size_t _maxCacheSize;
static inline const std::vector<std::filesystem::path> _fileExtensions{ ".jpg", ".jpeg", ".png", ".bmp" }; // TODO parametrize
const std::size_t _maxFileSize;
const std::vector<std::string> _preferredFileNames;
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ImageCache.hpp"
#include <mutex>
#include "core/Random.hpp"
#include "core/ILogger.hpp"
namespace lms::cover
{
ImageCache::ImageCache(std::size_t maxCacheSize)
: _maxCacheSize{ maxCacheSize }
{}
void ImageCache::addImage(const EntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image)
{
const std::unique_lock lock{ _mutex };
while (_cacheSize + image->getDataSize() > _maxCacheSize && !_cache.empty())
{
auto itRandom{ core::random::pickRandom(_cache) };
_cacheSize -= itRandom->second->getDataSize();
_cache.erase(itRandom);
}
_cacheSize += image->getDataSize();
_cache[entryDesc] = image;
}
std::shared_ptr<image::IEncodedImage> ImageCache::getImage(const EntryDesc& entryDesc) const
{
const std::shared_lock lock{ _mutex };
const auto it{ _cache.find(entryDesc) };
if (it == std::cend(_cache))
{
++_cacheMisses;
return nullptr;
}
++_cacheHits;
return it->second;
}
void ImageCache::flush()
{
const std::unique_lock lock{ _mutex };
LMS_LOG(COVER, DEBUG, "Cache stats: hits = " << _cacheHits.load() << ", misses = " << _cacheMisses.load() << ", nb entries = " << _cache.size() << ", size = " << _cacheSize);
_cacheHits = 0;
_cacheMisses = 0;
_cacheSize = 0;
_cache.clear();
}
}
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2015 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <atomic>
#include <shared_mutex>
#include <unordered_map>
#include <variant>
#include "database/ArtistId.hpp"
#include "database/ReleaseId.hpp"
#include "database/TrackId.hpp"
#include "image/IEncodedImage.hpp"
namespace lms::cover
{
class ImageCache
{
public:
ImageCache(std::size_t maxCacheSize);
struct EntryDesc
{
using VariantType = std::variant<db::ArtistId, db::ReleaseId, db::TrackId>;
VariantType id;
std::size_t size;
bool operator==(const EntryDesc& other) const = default;
};
std::size_t getMaxCacheSize() const { return _maxCacheSize; }
void addImage(const EntryDesc& entryDesc, std::shared_ptr<image::IEncodedImage> image);
std::shared_ptr<image::IEncodedImage> getImage(const EntryDesc& entryDesc) const;
void flush();
private:
const std::size_t _maxCacheSize;
mutable std::shared_mutex _mutex;
struct EntryHasher
{
constexpr std::size_t operator()(const EntryDesc& entry) const
{
return std::hash<EntryDesc::VariantType>{}(entry.id) ^ std::hash<std::size_t>{}(entry.size);
}
};
std::unordered_map<EntryDesc, std::shared_ptr<image::IEncodedImage>, EntryHasher> _cache;
std::size_t _cacheSize{};
mutable std::atomic<std::size_t> _cacheMisses{};
mutable std::atomic<std::size_t> _cacheHits{};
};
}