¶ 编译
官方编译教程:https://cachelib.org/docs/installation/
Debian 11上的liburing版本是0.7-3,好像太老了,编译会报错
error: ‘IORING_SETUP_SQE128’ was not declared in this scope
把它删掉就好了:
sudo apt remove liburing1 liburing-dev
然后编译:
./contrib/build.sh -j
编译结果默认存在./opt/cachelib
里。如果要编译安装到另一个目录,比如$HOME/opt/cachelib
:
./contrib/build.sh -j -p $HOME/opt/cachelib
注意,编译安装好了之后是不能移动的,因为里面有些so文件中硬编码了RUNPATH,比如:
% readelf -d libthriftprotocol.so.1.0.0 | grep PATH
0x000000000000001d (RUNPATH) Library runpath: [/home/searchstar/opt/cachelib/lib:/usr/local/lib]
移动了位置之后就会发生library not found的错误:解决找不到RUNPATH下的库的问题
¶ 存在的问题
¶ gcc internal error
Debian
11上的gcc版本为10.2.1 20210110
,太老了。如果出现了internal
error的话,可以用nix
安装新版本的gcc10
:
nix-env -iA nixpkgs.gcc10
nix
包管理器教程:使用国内源安装和使用Nix包管理器
¶ 配置环境变量
把编译安装的目录的绝对路径存入CACHELIB_HOME
里,然后在~/.profile
里加入如下内容来将其头文件和库暴露出去:
export CPLUS_INCLUDE_PATH=$CACHELIB_HOME/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=$CACHELIB_HOME/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=$CACHELIB_HOME/lib:$LD_LIBRARY_PATH
export CMAKE_PREFIX_PATH=$CACHELIB_HOME/lib/cmake:$CMAKE_PREFIX_PATH
然后source ~/.profile
¶ 使用
官方文档:https://cachelib.org/docs/
¶ CMake
官方文档里似乎没写。
find_package(cachelib REQUIRED)
target_link_libraries(${PROJECT_NAME}
PRIVATE
cachelib )
# Additional "FindXXX.cmake" files are here (e.g. FindSodium.cmake)
CLCMAKE="$workspace/CacheLib/cachelib/cmake"
CMAKE_PARAMS="-DCMAKE_MODULE_PATH='$CLCMAKE'"
mkdir -p build
cd build
cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo $CMAKE_PARAMS
make
cd ..
¶ 头文件
#include <cachelib/allocator/CacheAllocator.h>
¶
Hybrid Cache
::cachelib::LruAllocator::Config lruConfig;
facebook::cachelib::LruAllocator::NvmCacheConfig nvmConfig;
facebook.navyConfig.setBlockSize(4096);
nvmConfig.navyConfig.setSimpleFile(
nvmConfig.db_paths[0].path + "/cachelib",
options,
cachelib_sizetrue /*truncateFile*/);
.navyConfig.blockCache().setRegionSize(16 * 1024 * 1024);
nvmConfig.enableNvmCache(nvmConfig);
lruConfig.setAccessConfig({/*bucketsPower*/ 25, /*locksPower*/ 10})
lruConfig.validate();
::cachelib::LruAllocator cache(lruConfig);
facebookauto poolId =
.addPool("default_pool", cache.getCacheMemoryStats().ramCacheSize); cache
bucketsPower
默认是10,也就是默认1024个bucket,locksPower
默认是5,也就是32个lock,都太小了,一定要手动调成更大的值。bucketsPower
取值教程:https://cachelib.org/docs/Cache_Library_User_Guides/Configure_HashTable#choosing-a-good-value
¶ 常用方法
allocate(poolId, key, value_size)
:
返回WriteHandle
WriteHandle::getMemory
,
WriteHandle::getSize
:
value的内存起始地址和大小。可以用memcpy
之类的往这块内存里面写东西。但好像不能更改大小。
insertOrReplace(WriteHandle)
find(key)
: 返回ReadHandle
findToWrite
: 返回WriteHandle
检查ReadHandle
或者WriteHandle
是否有效:if (handle) {
ReadHandle::getMemory
, ReadHandle::getSize
:
value的内存起始地址和大小。只读。
remove(key)