parent
f7b49a5beb
commit
d04e0234f2
@ -0,0 +1,179 @@
|
||||
#include "FileCache.h"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
FileCache::FileCache(const std::string& cache_dir, size_t max_memory_items)
|
||||
: cache_dir_(cache_dir), max_memory_items_(max_memory_items)
|
||||
{
|
||||
fs::create_directories(cache_dir_);
|
||||
}
|
||||
|
||||
FileCache::~FileCache()
|
||||
{
|
||||
// Clean up memory-mapped files
|
||||
for (auto& [key, mapped_file] : mapped_files_) {
|
||||
if (mapped_file.data) {
|
||||
unmap_file(mapped_file.data, mapped_file.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileCache::hash_key(const std::string& key) const
|
||||
{
|
||||
// Simple hash function (replace with a better one if needed)
|
||||
std::hash<std::string> hasher;
|
||||
size_t hash_value = hasher(key);
|
||||
std::ostringstream hex_stream;
|
||||
hex_stream << std::hex << hash_value;
|
||||
return hex_stream.str();
|
||||
}
|
||||
|
||||
std::string FileCache::get_file_path(const std::string& key) const
|
||||
{
|
||||
std::string hashed = hash_key(key);
|
||||
std::string sub_dir = cache_dir_ + "/" + hashed.substr(0, 2);
|
||||
fs::create_directories(sub_dir);
|
||||
return sub_dir + "/" + hashed.substr(2) + ".cache";
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> FileCache::get(const std::string& key)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
if (memory_cache_.count(key)) {
|
||||
const auto& data = memory_cache_[key];
|
||||
return *reinterpret_cast<const T*>(data.data());
|
||||
}
|
||||
|
||||
if (mapped_files_.count(key)) {
|
||||
const auto& mapped_file = mapped_files_[key];
|
||||
return *reinterpret_cast<const T*>(mapped_file.data);
|
||||
}
|
||||
}
|
||||
|
||||
auto file_path = get_file_path(key);
|
||||
return load_from_disk<T>(file_path);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void FileCache::set(const std::string& key, const T& data)
|
||||
{
|
||||
auto serialized = std::vector<uint8_t>(reinterpret_cast<const uint8_t*>(&data),
|
||||
reinterpret_cast<const uint8_t*>(&data) + sizeof(T));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_[key] = serialized;
|
||||
|
||||
// Evict if necessary
|
||||
if (memory_cache_.size() > max_memory_items_)
|
||||
{
|
||||
memory_cache_.erase(memory_cache_.begin());
|
||||
}
|
||||
}
|
||||
|
||||
// Async save to disk
|
||||
auto file_path = get_file_path(key);
|
||||
async_tasks_.push_back(std::async(std::launch::async, [this, file_path, serialized]() {
|
||||
save_to_disk(file_path, serialized);
|
||||
}));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool FileCache::save_to_disk(const std::string& file_path, const T& data)
|
||||
{
|
||||
std::ofstream out(file_path, std::ios::binary);
|
||||
if (!out) return false;
|
||||
|
||||
out.write(reinterpret_cast<const char*>(&data), sizeof(data));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> FileCache::load_from_disk(const std::string& file_path) const
|
||||
{
|
||||
size_t file_size = fs::file_size(file_path);
|
||||
void* mapped_data = map_file(file_path, file_size);
|
||||
if (!mapped_data)
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Store the mapped file for later use
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
mapped_files_[file_path] = {mapped_data, file_size};
|
||||
|
||||
return *reinterpret_cast<const T*>(mapped_data);
|
||||
}
|
||||
|
||||
void FileCache::clear()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cache_mutex_);
|
||||
memory_cache_.clear();
|
||||
for (const auto& [key, mapped_file] : mapped_files_) {
|
||||
if (mapped_file.data)
|
||||
{
|
||||
unmap_file(mapped_file.data, mapped_file.size);
|
||||
}
|
||||
}
|
||||
mapped_files_.clear();
|
||||
for (const auto& entry : fs::directory_iterator(cache_dir_))
|
||||
{
|
||||
fs::remove_all(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* FileCache::map_file(const std::string& file_path, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
HANDLE file = CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (file == INVALID_HANDLE_VALUE) return nullptr;
|
||||
|
||||
HANDLE mapping = CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
||||
if (!mapping)
|
||||
{
|
||||
CloseHandle(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* data = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, size);
|
||||
CloseHandle(mapping);
|
||||
CloseHandle(file);
|
||||
return data;
|
||||
#else
|
||||
int fd = open(file_path.c_str(), O_RDONLY);
|
||||
if (fd == -1) return nullptr;
|
||||
|
||||
void* data = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
|
||||
if (data == MAP_FAILED) {
|
||||
return nullptr;
|
||||
}
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileCache::unmap_file(void* data, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
UnmapViewOfFile(data);
|
||||
#else
|
||||
munmap(data, size);
|
||||
#endif
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
class FileCache
|
||||
{
|
||||
public:
|
||||
FileCache(const std::string& cache_dir = "./tile_cache", size_t max_memory_items = 1000);
|
||||
~FileCache();
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> get(const std::string& key);
|
||||
|
||||
template<typename T>
|
||||
void set(const std::string& key, const T& data);
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
std::string get_file_path(const std::string& key) const;
|
||||
std::string hash_key(const std::string& key) const;
|
||||
|
||||
template<typename T>
|
||||
bool save_to_disk(const std::string& file_path, const T& data);
|
||||
|
||||
template<typename T>
|
||||
std::optional<T> load_from_disk(const std::string& file_path) const;
|
||||
|
||||
std::string cache_dir_;
|
||||
size_t max_memory_items_;
|
||||
|
||||
// Memory cache
|
||||
mutable std::mutex cache_mutex_;
|
||||
std::unordered_map<std::string, std::vector<uint8_t>> memory_cache_;
|
||||
|
||||
// Memory-mapped files
|
||||
struct MappedFile {
|
||||
void* data;
|
||||
size_t size;
|
||||
};
|
||||
std::unordered_map<std::string, MappedFile> mapped_files_;
|
||||
|
||||
// Async tasks
|
||||
std::vector<std::future<void>> async_tasks_;
|
||||
|
||||
// Platform-specific memory mapping
|
||||
void* map_file(const std::string& file_path, size_t size);
|
||||
void unmap_file(void* data, size_t size);
|
||||
};
|
Loading…
Reference in new issue