-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.cpp
More file actions
107 lines (92 loc) · 3.41 KB
/
main.cpp
File metadata and controls
107 lines (92 loc) · 3.41 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cachelib/allocator/CacheAllocator.h"
#include "cachelib/allocator/MemoryTierCacheConfig.h"
#include "folly/init/Init.h"
namespace facebook {
namespace cachelib_examples {
using Cache = cachelib::LruAllocator; // or Lru2QAllocator, or TinyLFUAllocator
using CacheConfig = typename Cache::Config;
using CacheKey = typename Cache::Key;
using CacheItemHandle = typename Cache::ReadHandle;
using MemoryTierCacheConfig = typename cachelib::MemoryTierCacheConfig;
// Global cache object and a default cache pool
std::unique_ptr<Cache> gCache_;
cachelib::PoolId defaultPool_;
void initializeCache() {
CacheConfig config;
config
.setCacheSize(48 * 1024 * 1024) // 48 MB
.setCacheName("MultiTier Cache")
.enableCachePersistence("/tmp")
.setAccessConfig(
{25 /* bucket power */, 10 /* lock power */}) // assuming caching 20
// million items
.configureMemoryTiers({
MemoryTierCacheConfig::fromShm().setRatio(1),
MemoryTierCacheConfig::fromFile("/pmem/file1").setRatio(2)})
.validate(); // will throw if bad config
gCache_ = std::make_unique<Cache>(Cache::SharedMemNew, config);
defaultPool_ =
gCache_->addPool("default", gCache_->getCacheMemoryStats().cacheSize);
}
void destroyCache() { gCache_.reset(); }
CacheItemHandle get(CacheKey key) { return gCache_->find(key); }
bool put(CacheKey key, const std::string& value) {
auto handle = gCache_->allocate(defaultPool_, key, value.size());
if (!handle) {
return false; // cache may fail to evict due to too many pending writes
}
std::memcpy(handle->getMemory(), value.data(), value.size());
gCache_->insertOrReplace(handle);
return true;
}
} // namespace cachelib_examples
} // namespace facebook
using namespace facebook::cachelib_examples;
int main(int argc, char** argv) {
folly::init(&argc, &argv);
initializeCache();
std::string value(4*1024, 'X'); // 4 KB value
const size_t NUM_ITEMS = 13000;
// Use cache
{
for(size_t i = 0; i < NUM_ITEMS; ++i) {
std::string key = "key" + std::to_string(i);
auto res = put(key, value);
std::ignore = res;
assert(res);
}
size_t nFound = 0;
size_t nNotFound = 0;
for(size_t i = 0; i < NUM_ITEMS; ++i) {
std::string key = "key" + std::to_string(i);
auto item = get(key);
if(item) {
++nFound;
folly::StringPiece sp{reinterpret_cast<const char*>(item->getMemory()),
item->getSize()};
std::ignore = sp;
assert(sp == value);
} else {
++nNotFound;
}
}
std::cout << "Found:\t\t" << nFound << " items\n"
<< "Not found:\t" << nNotFound << " items" << std::endl;
}
destroyCache();
}