@@ -35,47 +35,54 @@ size_t PrefixCacheWithUpload::insert(const Slice<int32_t>& token_ids,
3535 std::vector<Block>& blocks) {
3636 std::vector<Murmur3Key> insert_keys;
3737 auto n_tokens = PrefixCache::insert (token_ids, blocks, &insert_keys);
38+ save_event_async (true , insert_keys);
39+ return n_tokens;
40+ }
3841
39- threadpool_.schedule ([insert_keys = std::move (insert_keys), this ]() {
40- auto front_ptr = this ->db_kvcache_events_ .get_front_value ();
41- if (!front_ptr) {
42- LOG (INFO) << " Front DoubleBufferKvCacheEvent is nullptr!" ;
43- return ;
44- }
45- if (!this ->exited_ .load ()) {
46- for (const auto & hash_id : insert_keys) {
47- front_ptr->removed_cache .erase (hash_id);
48- front_ptr->stored_cache .insert (hash_id);
49- }
50- }
51- });
52-
42+ size_t PrefixCacheWithUpload::insert (const std::vector<Block>& blocks) {
43+ std::vector<Murmur3Key> insert_keys;
44+ auto n_tokens = PrefixCache::insert (blocks, &insert_keys);
45+ save_event_async (true , insert_keys);
5346 return n_tokens;
5447}
5548
5649size_t PrefixCacheWithUpload::evict (size_t n_blocks) {
5750 std::vector<Murmur3Key> evict_keys;
5851 auto evict_count = PrefixCache::evict (n_blocks, &evict_keys);
52+ save_event_async (false , evict_keys);
53+ return evict_count;
54+ }
5955
60- threadpool_.schedule ([evict_keys = std::move (evict_keys), this ]() {
56+ void PrefixCacheWithUpload::save_event_async (const bool is_insert,
57+ std::vector<Murmur3Key>& keys) {
58+ threadpool_.schedule ([this , is_insert = is_insert, keys = std::move (keys)]() {
59+ std::lock_guard<std::mutex> lock (this ->mutex_ );
6160 auto front_ptr = this ->db_kvcache_events_ .get_front_value ();
6261 if (!front_ptr) {
6362 LOG (INFO) << " Front DoubleBufferKvCacheEvent is nullptr!" ;
6463 return ;
6564 }
6665 if (!this ->exited_ .load ()) {
67- for (const auto & hash_id : evict_keys) {
68- front_ptr->removed_cache .insert (hash_id);
69- front_ptr->stored_cache .erase (hash_id);
66+ if (is_insert) {
67+ for (const auto & key : keys) {
68+ front_ptr->removed_cache .erase (key);
69+ front_ptr->stored_cache .insert (key);
70+ }
71+ } else {
72+ for (const auto & key : keys) {
73+ front_ptr->removed_cache .insert (key);
74+ front_ptr->stored_cache .erase (key);
75+ }
7076 }
7177 }
7278 });
73-
74- return evict_count;
7579}
7680
7781KvCacheEvent* PrefixCacheWithUpload::get_upload_kvcache_events () {
78- db_kvcache_events_.swap ();
82+ {
83+ std::lock_guard<std::mutex> lock (this ->mutex_ );
84+ db_kvcache_events_.swap ();
85+ }
7986 if (!exited_.load ()) {
8087 return db_kvcache_events_.get_back_value ();
8188 } else {
0 commit comments