Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
milvus
提交
46455352
milvus
项目概览
BaiXuePrincess
/
milvus
与 Fork 源项目一致
从无法访问的项目Fork
通知
7
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
milvus
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
46455352
编写于
9月 27, 2019
作者:
S
starlord
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
format cache code
Former-commit-id: 99ff6affb78c09941a59c53183427a14b8ff00d1
上级
6ac9c1a0
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
184 addition
and
134 deletion
+184
-134
cpp/src/cache/Cache.h
cpp/src/cache/Cache.h
+25
-15
cpp/src/cache/Cache.inl
cpp/src/cache/Cache.inl
+31
-22
cpp/src/cache/CacheMgr.h
cpp/src/cache/CacheMgr.h
+14
-12
cpp/src/cache/CacheMgr.inl
cpp/src/cache/CacheMgr.inl
+34
-24
cpp/src/cache/CpuCacheMgr.cpp
cpp/src/cache/CpuCacheMgr.cpp
+14
-10
cpp/src/cache/CpuCacheMgr.h
cpp/src/cache/CpuCacheMgr.h
+10
-7
cpp/src/cache/DataObj.h
cpp/src/cache/DataObj.h
+21
-16
cpp/src/cache/GpuCacheMgr.cpp
cpp/src/cache/GpuCacheMgr.cpp
+15
-11
cpp/src/cache/GpuCacheMgr.h
cpp/src/cache/GpuCacheMgr.h
+8
-7
cpp/src/cache/LRU.h
cpp/src/cache/LRU.h
+12
-10
未找到文件。
cpp/src/cache/Cache.h
浏览文件 @
46455352
...
...
@@ -32,30 +32,40 @@ namespace cache {
template
<
typename
ItemObj
>
class
Cache
{
public:
public:
//mem_capacity, units:GB
Cache
(
int64_t
capacity_gb
,
uint64_t
cache_max_count
);
~
Cache
()
=
default
;
int64_t
usage
()
const
{
return
usage_
;
}
int64_t
capacity
()
const
{
return
capacity_
;
}
//unit: BYTE
int64_t
usage
()
const
{
return
usage_
;
}
int64_t
capacity
()
const
{
return
capacity_
;
}
//unit: BYTE
void
set_capacity
(
int64_t
capacity
);
//unit: BYTE
double
freemem_percent
()
const
{
return
freemem_percent_
;
};
void
set_freemem_percent
(
double
percent
)
{
freemem_percent_
=
percent
;
}
double
freemem_percent
()
const
{
return
freemem_percent_
;
}
void
set_freemem_percent
(
double
percent
)
{
freemem_percent_
=
percent
;
}
size_t
size
()
const
;
bool
exists
(
const
std
::
string
&
key
);
ItemObj
get
(
const
std
::
string
&
key
);
void
insert
(
const
std
::
string
&
key
,
const
ItemObj
&
item
);
void
erase
(
const
std
::
string
&
key
);
bool
exists
(
const
std
::
string
&
key
);
ItemObj
get
(
const
std
::
string
&
key
);
void
insert
(
const
std
::
string
&
key
,
const
ItemObj
&
item
);
void
erase
(
const
std
::
string
&
key
);
void
print
();
void
clear
();
private:
private:
void
free_memory
();
private:
private:
int64_t
usage_
;
int64_t
capacity_
;
double
freemem_percent_
;
...
...
@@ -64,8 +74,8 @@ private:
mutable
std
::
mutex
mutex_
;
};
}
//
cache
}
//
milvus
}
//
zilliz
}
// namespace
cache
}
// namespace
milvus
}
// namespace
zilliz
#include "cache/Cache.inl"
\ No newline at end of file
#include "cache/Cache.inl"
cpp/src/cache/Cache.inl
浏览文件 @
46455352
...
...
@@ -33,29 +33,33 @@ Cache<ItemObj>::Cache(int64_t capacity, uint64_t cache_max_count)
}
template<typename ItemObj>
void Cache<ItemObj>::set_capacity(int64_t capacity) {
if(capacity > 0) {
void
Cache<ItemObj>::set_capacity(int64_t capacity) {
if (capacity > 0) {
capacity_ = capacity;
free_memory();
}
}
template<typename ItemObj>
size_t Cache<ItemObj>::size() const {
size_t
Cache<ItemObj>::size() const {
std::lock_guard<std::mutex> lock(mutex_);
return lru_.size();
}
template<typename ItemObj>
bool Cache<ItemObj>::exists(const std::string& key) {
bool
Cache<ItemObj>::exists(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_);
return lru_.exists(key);
}
template<typename ItemObj>
ItemObj Cache<ItemObj>::get(const std::string& key) {
ItemObj
Cache<ItemObj>::get(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_);
if
(!lru_.exists(key))
{
if
(!lru_.exists(key))
{
return nullptr;
}
...
...
@@ -63,8 +67,9 @@ ItemObj Cache<ItemObj>::get(const std::string& key) {
}
template<typename ItemObj>
void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
if(item == nullptr) {
void
Cache<ItemObj>::insert(const std::string &key, const ItemObj &item) {
if (item == nullptr) {
return;
}
...
...
@@ -80,7 +85,7 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
//if key already exist, subtract old item size
if (lru_.exists(key)) {
const ItemObj
&
old_item = lru_.get(key);
const ItemObj
&
old_item = lru_.get(key);
usage_ -= old_item->size();
}
...
...
@@ -107,13 +112,14 @@ void Cache<ItemObj>::insert(const std::string& key, const ItemObj& item) {
}
template<typename ItemObj>
void Cache<ItemObj>::erase(const std::string& key) {
void
Cache<ItemObj>::erase(const std::string &key) {
std::lock_guard<std::mutex> lock(mutex_);
if
(!lru_.exists(key))
{
if
(!lru_.exists(key))
{
return;
}
const ItemObj
&
old_item = lru_.get(key);
const ItemObj
&
old_item = lru_.get(key);
usage_ -= old_item->size();
SERVER_LOG_DEBUG << "Erase " << key << " size: " << old_item->size();
...
...
@@ -122,7 +128,8 @@ void Cache<ItemObj>::erase(const std::string& key) {
}
template<typename ItemObj>
void Cache<ItemObj>::clear() {
void
Cache<ItemObj>::clear() {
std::lock_guard<std::mutex> lock(mutex_);
lru_.clear();
usage_ = 0;
...
...
@@ -131,12 +138,13 @@ void Cache<ItemObj>::clear() {
/* free memory space when CACHE occupation exceed its capacity */
template<typename ItemObj>
void Cache<ItemObj>::free_memory() {
void
Cache<ItemObj>::free_memory() {
if (usage_ <= capacity_) return;
int64_t threshhold = capacity_ * freemem_percent_;
int64_t delta_size = usage_ - threshhold;
if(delta_size <= 0) {
if
(delta_size <= 0) {
delta_size = 1;//ensure at least one item erased
}
...
...
@@ -148,8 +156,8 @@ void Cache<ItemObj>::free_memory() {
auto it = lru_.rbegin();
while (it != lru_.rend() && released_size < delta_size) {
auto
&
key = it->first;
auto
&
obj_ptr = it->second;
auto
&
key = it->first;
auto
&
obj_ptr = it->second;
key_array.emplace(key);
released_size += obj_ptr->size();
...
...
@@ -159,7 +167,7 @@ void Cache<ItemObj>::free_memory() {
SERVER_LOG_DEBUG << "to be released memory size: " << released_size;
for (auto
&
key : key_array) {
for (auto
&
key : key_array) {
erase(key);
}
...
...
@@ -167,7 +175,8 @@ void Cache<ItemObj>::free_memory() {
}
template<typename ItemObj>
void Cache<ItemObj>::print() {
void
Cache<ItemObj>::print() {
size_t cache_count = 0;
{
std::lock_guard<std::mutex> lock(mutex_);
...
...
@@ -179,7 +188,7 @@ void Cache<ItemObj>::print() {
SERVER_LOG_DEBUG << "[Cache capacity]: " << capacity_ << " bytes";
}
}
//
cache
}
//
milvus
}
//
zilliz
}
// namespace
cache
}
// namespace
milvus
}
// namespace
zilliz
cpp/src/cache/CacheMgr.h
浏览文件 @
46455352
...
...
@@ -22,22 +22,25 @@
#include "utils/Log.h"
#include "metrics/Metrics.h"
#include <string>
#include <memory>
namespace
zilliz
{
namespace
milvus
{
namespace
cache
{
template
<
typename
ItemObj
>
class
CacheMgr
{
public:
public:
virtual
uint64_t
ItemCount
()
const
;
virtual
bool
ItemExists
(
const
std
::
string
&
key
);
virtual
bool
ItemExists
(
const
std
::
string
&
key
);
virtual
ItemObj
GetItem
(
const
std
::
string
&
key
);
virtual
ItemObj
GetItem
(
const
std
::
string
&
key
);
virtual
void
InsertItem
(
const
std
::
string
&
key
,
const
ItemObj
&
data
);
virtual
void
InsertItem
(
const
std
::
string
&
key
,
const
ItemObj
&
data
);
virtual
void
EraseItem
(
const
std
::
string
&
key
);
virtual
void
EraseItem
(
const
std
::
string
&
key
);
virtual
void
PrintInfo
();
...
...
@@ -47,18 +50,17 @@ public:
int64_t
CacheCapacity
()
const
;
void
SetCapacity
(
int64_t
capacity
);
protected:
protected:
CacheMgr
();
virtual
~
CacheMgr
();
protected:
protected:
using
CachePtr
=
std
::
shared_ptr
<
Cache
<
ItemObj
>>
;
CachePtr
cache_
;
};
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
}
}
}
#include "cache/CacheMgr.inl"
\ No newline at end of file
#include "cache/CacheMgr.inl"
cpp/src/cache/CacheMgr.inl
浏览文件 @
46455352
...
...
@@ -30,18 +30,20 @@ CacheMgr<ItemObj>::~CacheMgr() {
}
template<typename ItemObj>
uint64_t CacheMgr<ItemObj>::ItemCount() const {
if(cache_ == nullptr) {
uint64_t
CacheMgr<ItemObj>::ItemCount() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return 0;
}
return (uint64_t)(cache_->size());
return (uint64_t)
(cache_->size());
}
template<typename ItemObj>
bool CacheMgr<ItemObj>::ItemExists(const std::string& key) {
if(cache_ == nullptr) {
bool
CacheMgr<ItemObj>::ItemExists(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return false;
}
...
...
@@ -50,8 +52,9 @@ bool CacheMgr<ItemObj>::ItemExists(const std::string& key) {
}
template<typename ItemObj>
ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) {
if(cache_ == nullptr) {
ItemObj
CacheMgr<ItemObj>::GetItem(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return nullptr;
}
...
...
@@ -60,8 +63,9 @@ ItemObj CacheMgr<ItemObj>::GetItem(const std::string& key) {
}
template<typename ItemObj>
void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data) {
if(cache_ == nullptr) {
void
CacheMgr<ItemObj>::InsertItem(const std::string &key, const ItemObj &data) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
...
...
@@ -71,8 +75,9 @@ void CacheMgr<ItemObj>::InsertItem(const std::string& key, const ItemObj& data)
}
template<typename ItemObj>
void CacheMgr<ItemObj>::EraseItem(const std::string& key) {
if(cache_ == nullptr) {
void
CacheMgr<ItemObj>::EraseItem(const std::string &key) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
...
...
@@ -82,8 +87,9 @@ void CacheMgr<ItemObj>::EraseItem(const std::string& key) {
}
template<typename ItemObj>
void CacheMgr<ItemObj>::PrintInfo() {
if(cache_ == nullptr) {
void
CacheMgr<ItemObj>::PrintInfo() {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
...
...
@@ -92,8 +98,9 @@ void CacheMgr<ItemObj>::PrintInfo() {
}
template<typename ItemObj>
void CacheMgr<ItemObj>::ClearCache() {
if(cache_ == nullptr) {
void
CacheMgr<ItemObj>::ClearCache() {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
...
...
@@ -102,8 +109,9 @@ void CacheMgr<ItemObj>::ClearCache() {
}
template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheUsage() const {
if(cache_ == nullptr) {
int64_t
CacheMgr<ItemObj>::CacheUsage() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return 0;
}
...
...
@@ -112,8 +120,9 @@ int64_t CacheMgr<ItemObj>::CacheUsage() const {
}
template<typename ItemObj>
int64_t CacheMgr<ItemObj>::CacheCapacity() const {
if(cache_ == nullptr) {
int64_t
CacheMgr<ItemObj>::CacheCapacity() const {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return 0;
}
...
...
@@ -122,14 +131,15 @@ int64_t CacheMgr<ItemObj>::CacheCapacity() const {
}
template<typename ItemObj>
void CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
if(cache_ == nullptr) {
void
CacheMgr<ItemObj>::SetCapacity(int64_t capacity) {
if (cache_ == nullptr) {
SERVER_LOG_ERROR << "Cache doesn't exist";
return;
}
cache_->set_capacity(capacity);
}
}
}
}
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/CpuCacheMgr.cpp
浏览文件 @
46455352
...
...
@@ -16,20 +16,22 @@
// under the License.
#include "CpuCacheMgr.h"
#include "
cache/
CpuCacheMgr.h"
#include "server/Config.h"
#include "utils/Log.h"
#include <utility>
namespace
zilliz
{
namespace
milvus
{
namespace
cache
{
namespace
{
constexpr
int64_t
unit
=
1024
*
1024
*
1024
;
constexpr
int64_t
unit
=
1024
*
1024
*
1024
;
}
CpuCacheMgr
::
CpuCacheMgr
()
{
server
::
Config
&
config
=
server
::
Config
::
GetInstance
();
server
::
Config
&
config
=
server
::
Config
::
GetInstance
();
Status
s
;
int32_t
cpu_mem_cap
;
...
...
@@ -38,7 +40,7 @@ CpuCacheMgr::CpuCacheMgr() {
SERVER_LOG_ERROR
<<
s
.
message
();
}
int64_t
cap
=
cpu_mem_cap
*
unit
;
cache_
=
std
::
make_shared
<
Cache
<
DataObjPtr
>>
(
cap
,
1UL
<<
32
);
cache_
=
std
::
make_shared
<
Cache
<
DataObjPtr
>>
(
cap
,
1UL
<<
32
);
float
cpu_mem_threshold
;
s
=
config
.
GetCacheConfigCpuMemThreshold
(
cpu_mem_threshold
);
...
...
@@ -53,20 +55,22 @@ CpuCacheMgr::CpuCacheMgr() {
}
}
CpuCacheMgr
*
CpuCacheMgr
::
GetInstance
()
{
CpuCacheMgr
*
CpuCacheMgr
::
GetInstance
()
{
static
CpuCacheMgr
s_mgr
;
return
&
s_mgr
;
}
engine
::
VecIndexPtr
CpuCacheMgr
::
GetIndex
(
const
std
::
string
&
key
)
{
engine
::
VecIndexPtr
CpuCacheMgr
::
GetIndex
(
const
std
::
string
&
key
)
{
DataObjPtr
obj
=
GetItem
(
key
);
if
(
obj
!=
nullptr
)
{
if
(
obj
!=
nullptr
)
{
return
obj
->
data
();
}
return
nullptr
;
}
}
}
}
\ No newline at end of file
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/CpuCacheMgr.h
浏览文件 @
46455352
...
...
@@ -20,21 +20,24 @@
#include "CacheMgr.h"
#include "DataObj.h"
#include <string>
#include <memory>
namespace
zilliz
{
namespace
milvus
{
namespace
cache
{
class
CpuCacheMgr
:
public
CacheMgr
<
DataObjPtr
>
{
private:
private:
CpuCacheMgr
();
public:
public:
//TODO: use smart pointer instead
static
CpuCacheMgr
*
GetInstance
();
static
CpuCacheMgr
*
GetInstance
();
engine
::
VecIndexPtr
GetIndex
(
const
std
::
string
&
key
);
engine
::
VecIndexPtr
GetIndex
(
const
std
::
string
&
key
);
};
}
}
}
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/DataObj.h
浏览文件 @
46455352
...
...
@@ -27,38 +27,43 @@ namespace milvus {
namespace
cache
{
class
DataObj
{
public:
DataObj
(
const
engine
::
VecIndexPtr
&
index
)
:
index_
(
index
)
{
}
public:
explicit
DataObj
(
const
engine
::
VecIndexPtr
&
index
)
:
index_
(
index
)
{
}
DataObj
(
const
engine
::
VecIndexPtr
&
index
,
int64_t
size
)
:
index_
(
index
),
size_
(
size
)
{
}
DataObj
(
const
engine
::
VecIndexPtr
&
index
,
int64_t
size
)
:
index_
(
index
),
size_
(
size
)
{
}
engine
::
VecIndexPtr
data
()
{
return
index_
;
}
const
engine
::
VecIndexPtr
&
data
()
const
{
return
index_
;
}
engine
::
VecIndexPtr
data
()
{
return
index_
;
}
const
engine
::
VecIndexPtr
&
data
()
const
{
return
index_
;
}
int64_t
size
()
const
{
if
(
index_
==
nullptr
)
{
if
(
index_
==
nullptr
)
{
return
0
;
}
if
(
size_
>
0
)
{
if
(
size_
>
0
)
{
return
size_
;
}
return
index_
->
Count
()
*
index_
->
Dimension
()
*
sizeof
(
float
);
}
private:
private:
engine
::
VecIndexPtr
index_
=
nullptr
;
int64_t
size_
=
0
;
};
using
DataObjPtr
=
std
::
shared_ptr
<
DataObj
>
;
}
}
}
\ No newline at end of file
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/GpuCacheMgr.cpp
浏览文件 @
46455352
...
...
@@ -16,11 +16,13 @@
// under the License.
#include
<sstream>
#include
"cache/GpuCacheMgr.h"
#include "utils/Log.h"
#include "GpuCacheMgr.h"
#include "server/Config.h"
#include <sstream>
#include <utility>
namespace
zilliz
{
namespace
milvus
{
namespace
cache
{
...
...
@@ -29,11 +31,11 @@ std::mutex GpuCacheMgr::mutex_;
std
::
unordered_map
<
uint64_t
,
GpuCacheMgrPtr
>
GpuCacheMgr
::
instance_
;
namespace
{
constexpr
int64_t
G_BYTE
=
1024
*
1024
*
1024
;
constexpr
int64_t
G_BYTE
=
1024
*
1024
*
1024
;
}
GpuCacheMgr
::
GpuCacheMgr
()
{
server
::
Config
&
config
=
server
::
Config
::
GetInstance
();
server
::
Config
&
config
=
server
::
Config
::
GetInstance
();
Status
s
;
int32_t
gpu_mem_cap
;
...
...
@@ -42,7 +44,7 @@ GpuCacheMgr::GpuCacheMgr() {
SERVER_LOG_ERROR
<<
s
.
message
();
}
int32_t
cap
=
gpu_mem_cap
*
G_BYTE
;
cache_
=
std
::
make_shared
<
Cache
<
DataObjPtr
>>
(
cap
,
1UL
<<
32
);
cache_
=
std
::
make_shared
<
Cache
<
DataObjPtr
>>
(
cap
,
1UL
<<
32
);
float
gpu_mem_threshold
;
s
=
config
.
GetCacheConfigGpuMemThreshold
(
gpu_mem_threshold
);
...
...
@@ -57,7 +59,8 @@ GpuCacheMgr::GpuCacheMgr() {
}
}
GpuCacheMgr
*
GpuCacheMgr
::
GetInstance
(
uint64_t
gpu_id
)
{
GpuCacheMgr
*
GpuCacheMgr
::
GetInstance
(
uint64_t
gpu_id
)
{
if
(
instance_
.
find
(
gpu_id
)
==
instance_
.
end
())
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
if
(
instance_
.
find
(
gpu_id
)
==
instance_
.
end
())
{
...
...
@@ -70,15 +73,16 @@ GpuCacheMgr* GpuCacheMgr::GetInstance(uint64_t gpu_id) {
}
}
engine
::
VecIndexPtr
GpuCacheMgr
::
GetIndex
(
const
std
::
string
&
key
)
{
engine
::
VecIndexPtr
GpuCacheMgr
::
GetIndex
(
const
std
::
string
&
key
)
{
DataObjPtr
obj
=
GetItem
(
key
);
if
(
obj
!=
nullptr
)
{
if
(
obj
!=
nullptr
)
{
return
obj
->
data
();
}
return
nullptr
;
}
}
}
}
\ No newline at end of file
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/GpuCacheMgr.h
浏览文件 @
46455352
...
...
@@ -21,6 +21,7 @@
#include <unordered_map>
#include <memory>
#include <string>
namespace
zilliz
{
namespace
milvus
{
...
...
@@ -30,18 +31,18 @@ class GpuCacheMgr;
using
GpuCacheMgrPtr
=
std
::
shared_ptr
<
GpuCacheMgr
>
;
class
GpuCacheMgr
:
public
CacheMgr
<
DataObjPtr
>
{
public:
public:
GpuCacheMgr
();
static
GpuCacheMgr
*
GetInstance
(
uint64_t
gpu_id
);
static
GpuCacheMgr
*
GetInstance
(
uint64_t
gpu_id
);
engine
::
VecIndexPtr
GetIndex
(
const
std
::
string
&
key
);
engine
::
VecIndexPtr
GetIndex
(
const
std
::
string
&
key
);
private:
private:
static
std
::
mutex
mutex_
;
static
std
::
unordered_map
<
uint64_t
,
GpuCacheMgrPtr
>
instance_
;
};
}
}
}
}
// namespace cache
}
// namespace milvus
}
// namespace zilliz
cpp/src/cache/LRU.h
浏览文件 @
46455352
...
...
@@ -22,6 +22,7 @@
#include <list>
#include <cstddef>
#include <stdexcept>
#include <utility>
namespace
zilliz
{
namespace
milvus
{
...
...
@@ -29,14 +30,15 @@ namespace cache {
template
<
typename
key_t
,
typename
value_t
>
class
LRU
{
public:
public:
typedef
typename
std
::
pair
<
key_t
,
value_t
>
key_value_pair_t
;
typedef
typename
std
::
list
<
key_value_pair_t
>::
iterator
list_iterator_t
;
typedef
typename
std
::
list
<
key_value_pair_t
>::
reverse_iterator
reverse_list_iterator_t
;
LRU
(
size_t
max_size
)
:
max_size_
(
max_size
)
{}
explicit
LRU
(
size_t
max_size
)
:
max_size_
(
max_size
)
{
}
void
put
(
const
key_t
&
key
,
const
value_t
&
value
)
{
void
put
(
const
key_t
&
key
,
const
value_t
&
value
)
{
auto
it
=
cache_items_map_
.
find
(
key
);
cache_items_list_
.
push_front
(
key_value_pair_t
(
key
,
value
));
if
(
it
!=
cache_items_map_
.
end
())
{
...
...
@@ -53,7 +55,7 @@ public:
}
}
const
value_t
&
get
(
const
key_t
&
key
)
{
const
value_t
&
get
(
const
key_t
&
key
)
{
auto
it
=
cache_items_map_
.
find
(
key
);
if
(
it
==
cache_items_map_
.
end
())
{
throw
std
::
range_error
(
"There is no such key in cache"
);
...
...
@@ -63,7 +65,7 @@ public:
}
}
void
erase
(
const
key_t
&
key
)
{
void
erase
(
const
key_t
&
key
)
{
auto
it
=
cache_items_map_
.
find
(
key
);
if
(
it
!=
cache_items_map_
.
end
())
{
cache_items_list_
.
erase
(
it
->
second
);
...
...
@@ -71,7 +73,7 @@ public:
}
}
bool
exists
(
const
key_t
&
key
)
const
{
bool
exists
(
const
key_t
&
key
)
const
{
return
cache_items_map_
.
find
(
key
)
!=
cache_items_map_
.
end
();
}
...
...
@@ -101,14 +103,14 @@ public:
cache_items_map_
.
clear
();
}
private:
private:
std
::
list
<
key_value_pair_t
>
cache_items_list_
;
std
::
unordered_map
<
key_t
,
list_iterator_t
>
cache_items_map_
;
size_t
max_size_
;
list_iterator_t
iter_
;
};
}
//
cache
}
//
milvus
}
//
zilliz
}
// namespace
cache
}
// namespace
milvus
}
// namespace
zilliz
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录