aboutsummaryrefslogtreecommitdiff
path: root/zenserver/cache/cachestore.h
blob: 89c6396b8c03bbb1af76c9004542c01245557fdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/IoBuffer.h>
#include <zencore/iohash.h>
#include <zencore/thread.h>
#include <zencore/uid.h>
#include <zenstore/cas.h>
#include <compare>
#include <filesystem>
#include <unordered_map>

namespace zen {

class WideStringBuilderBase;
class CasStore;

}  // namespace zen

struct CacheValue
{
	zen::IoBuffer Value;
};

/******************************************************************************

  /$$   /$$/$$    /$$        /$$$$$$                   /$$
 | $$  /$$| $$   | $$       /$$__  $$                 | $$
 | $$ /$$/| $$   | $$      | $$  \__/ /$$$$$$  /$$$$$$| $$$$$$$  /$$$$$$
 | $$$$$/ |  $$ / $$/      | $$      |____  $$/$$_____| $$__  $$/$$__  $$
 | $$  $$  \  $$ $$/       | $$       /$$$$$$| $$     | $$  \ $| $$$$$$$$
 | $$\  $$  \  $$$/        | $$    $$/$$__  $| $$     | $$  | $| $$_____/
 | $$ \  $$  \  $/         |  $$$$$$|  $$$$$$|  $$$$$$| $$  | $|  $$$$$$$
 |__/  \__/   \_/           \______/ \_______/\_______|__/  |__/\_______/

 Basic Key-Value cache. No restrictions on keys, and values are always opaque
 binary blobs.

******************************************************************************/

class CacheStore
{
public:
	virtual bool Get(std::string_view Key, CacheValue& OutValue)	= 0;
	virtual void Put(std::string_view Key, const CacheValue& Value) = 0;
};

/** File system based implementation

	Emulates the behaviour of UE4 with regards to file system structure,
	and also adds a file corruption trailer to remain compatible with
	the file-system based implementation (this should be made configurable)

  */
class FileCacheStore : public CacheStore
{
public:
	FileCacheStore(const char* RootDir, const char* ReadRootDir = nullptr);
	~FileCacheStore();

	virtual bool Get(std::string_view Key, CacheValue& OutValue) override;
	virtual void Put(std::string_view Key, const CacheValue& Value) override;

private:
	std::filesystem::path m_RootDir;
	std::filesystem::path m_ReadRootDir;
	bool				  m_IsOk			= true;
	bool				  m_ReadRootIsValid = false;
};

class MemoryCacheStore : public CacheStore
{
public:
	MemoryCacheStore();
	~MemoryCacheStore();

	virtual bool Get(std::string_view Key, CacheValue& OutValue) override;
	virtual void Put(std::string_view Key, const CacheValue& Value) override;

private:
	zen::RwLock									   m_Lock;
	std::unordered_map<std::string, zen::IoBuffer> m_CacheMap;
};