Skip to content

Commit 9a7901e

Browse files
vinser52igchor
authored andcommitted
Adding example for multitiered cache
1 parent 5977cb9 commit 9a7901e

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required (VERSION 3.12)
16+
17+
project (cachelib-cmake-test-project VERSION 0.1)
18+
19+
find_package(cachelib CONFIG REQUIRED)
20+
21+
add_executable(multitier-cache-example main.cpp)
22+
23+
target_link_libraries(multitier-cache-example cachelib)

examples/multitier_cache/build.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Facebook, Inc. and its affiliates.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -e
18+
19+
# Root directory for the CacheLib project
20+
CLBASE="$PWD/../.."
21+
22+
# Additional "FindXXX.cmake" files are here (e.g. FindSodium.cmake)
23+
CLCMAKE="$CLBASE/cachelib/cmake"
24+
25+
# After ensuring we are in the correct directory, set the installation prefix"
26+
PREFIX="$CLBASE/opt/cachelib/"
27+
28+
CMAKE_PARAMS="-DCMAKE_INSTALL_PREFIX='$PREFIX' -DCMAKE_MODULE_PATH='$CLCMAKE'"
29+
30+
CMAKE_PREFIX_PATH="$PREFIX/lib/cmake:$PREFIX/lib64/cmake:$PREFIX/lib:$PREFIX/lib64:$PREFIX:${CMAKE_PREFIX_PATH:-}"
31+
export CMAKE_PREFIX_PATH
32+
PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/lib64/pkgconfig:${PKG_CONFIG_PATH:-}"
33+
export PKG_CONFIG_PATH
34+
LD_LIBRARY_PATH="$PREFIX/lib:$PREFIX/lib64:${LD_LIBRARY_PATH:-}"
35+
export LD_LIBRARY_PATH
36+
37+
mkdir -p build
38+
cd build
39+
cmake $CMAKE_PARAMS ..
40+
make

examples/multitier_cache/main.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cachelib/allocator/CacheAllocator.h"
18+
#include "cachelib/allocator/MemoryTierCacheConfig.h"
19+
#include "folly/init/Init.h"
20+
21+
namespace facebook {
22+
namespace cachelib_examples {
23+
using Cache = cachelib::LruAllocator; // or Lru2QAllocator, or TinyLFUAllocator
24+
using CacheConfig = typename Cache::Config;
25+
using CacheKey = typename Cache::Key;
26+
using CacheItemHandle = typename Cache::ItemHandle;
27+
using MemoryTierCacheConfig = typename cachelib::MemoryTierCacheConfig;
28+
29+
// Global cache object and a default cache pool
30+
std::unique_ptr<Cache> gCache_;
31+
cachelib::PoolId defaultPool_;
32+
33+
void initializeCache() {
34+
CacheConfig config;
35+
config
36+
.setCacheSize(48 * 1024 * 1024) // 48 MB
37+
.setCacheName("MultiTier Cache")
38+
.enableCachePersistence("/tmp")
39+
.setAccessConfig(
40+
{25 /* bucket power */, 10 /* lock power */}) // assuming caching 20
41+
// million items
42+
.configureMemoryTiers({
43+
MemoryTierCacheConfig::fromShm().setRatio(1),
44+
MemoryTierCacheConfig::fromFile("/tmp/file1").setRatio(2)})
45+
.validate(); // will throw if bad config
46+
gCache_ = std::make_unique<Cache>(Cache::SharedMemNew, config);
47+
defaultPool_ =
48+
gCache_->addPool("default", gCache_->getCacheMemoryStats().cacheSize);
49+
}
50+
51+
void destroyCache() { gCache_.reset(); }
52+
53+
CacheItemHandle get(CacheKey key) { return gCache_->find(key); }
54+
55+
bool put(CacheKey key, const std::string& value) {
56+
auto handle = gCache_->allocate(defaultPool_, key, value.size());
57+
if (!handle) {
58+
return false; // cache may fail to evict due to too many pending writes
59+
}
60+
std::memcpy(handle->getWritableMemory(), value.data(), value.size());
61+
gCache_->insertOrReplace(handle);
62+
return true;
63+
}
64+
} // namespace cachelib_examples
65+
} // namespace facebook
66+
67+
using namespace facebook::cachelib_examples;
68+
69+
int main(int argc, char** argv) {
70+
folly::init(&argc, &argv);
71+
72+
initializeCache();
73+
74+
std::string value(4*1024, 'X'); // 4 KB value
75+
const size_t NUM_ITEMS = 13000;
76+
77+
// Use cache
78+
{
79+
for(size_t i = 0; i < NUM_ITEMS; ++i) {
80+
std::string key = "key" + std::to_string(i);
81+
auto res = put(key, value);
82+
83+
std::ignore = res;
84+
assert(res);
85+
}
86+
87+
size_t nFound = 0;
88+
size_t nNotFound = 0;
89+
for(size_t i = 0; i < NUM_ITEMS; ++i) {
90+
std::string key = "key" + std::to_string(i);
91+
auto item = get(key);
92+
if(item) {
93+
++nFound;
94+
folly::StringPiece sp{reinterpret_cast<const char*>(item->getMemory()),
95+
item->getSize()};
96+
std::ignore = sp;
97+
assert(sp == value);
98+
} else {
99+
++nNotFound;
100+
}
101+
}
102+
std::cout << "Found:\t\t" << nFound << " items\n"
103+
<< "Not found:\t" << nNotFound << " items" << std::endl;
104+
}
105+
106+
destroyCache();
107+
}

0 commit comments

Comments
 (0)