-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLruCache.cpp
More file actions
138 lines (138 loc) · 3.34 KB
/
Copy pathLruCache.cpp
File metadata and controls
138 lines (138 loc) · 3.34 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//#pragma once
//#include"LruCache.h"
//#include<unordered_map>
//
//template<typename Key, typename Value>
//Cache::LruCache<Key, Value>::LruCache(size_t s)
// :CachePolicys<Key,Value>(s)
//{
// //进行初始化
// init();
//}
//
//template<typename Key, typename Value>
//void Cache::LruCache<Key, Value>::put(Key key, Value value)
//{
// std::lock_guard<std::mutex> lock(mutex_);
// //先检验这个key值是否存在
// auto it = this->valueMap.find(key);
// if (it != this->valueMap.end())//存在于map中
// {
// updateExistingNode(it->second, value);//直接更新
// return;
// }
// if (this->capacity == 0)return;//这个系统的容量是0
// if (this->valueMap.size()>= this->capacity)//map满了
// {
// evicLeastRecent();//删除一个结点
// }
// addNewNode(key, value);
//}
//
////获得缓存数据
//template<typename Key,typename Value>
//bool Cache::LruCache<Key,Value>::get(Key key, Value& value)
//{
// std::lock_guard<std::mutex> lock(mutex_);
// Value v;
// auto it = this->valueMap.find(key);
// if (it == this->valueMap.end())//不存在这个数据
// {
// value = v;
// return false;
// }
// else
// {
// v = it->second->getValue();
// value = v;
// return true;
// }
//}
//
//template<typename Key, typename Value>
//Value Cache::LruCache<Key,Value>::get(Key key)
//{
// std::lock_guard<std::mutex> lock(mutex_);
// Value v;
// auto it = this->valueMap.find(key);
// if (it == this->valueMap.end())
// {
// return v;
// }
// else
// {
// v = it->second->getValue();
// return v;
// }
//}
//
////初始化链表
//template<typename Key, typename Value>
//void Cache::LruCache<Key,Value>::init()
//{
// headPtr = std::make_shared<node>();
// tailPtr = std::make_shared<node>();
// headPtr->next = tailPtr;
// tailPtr->pre = tailPtr;
//}
//
////添加新结点
//template<typename Key,typename Value>
//void Cache::LruCache<Key,Value>::addNewNode(const Key& key, const Value& value)
//{
// NodePtr newP = std::make_shared<node>(key, value);
// this->valueMap.insert(newP);
// insertList(newP);//将结点插入到链表当中
//}
//
////更新现有结点
//template<typename Key, typename Value>
//void Cache::LruCache<Key,Value>::updateExistingNode(NodePtr p, const Value& value)
//{
// p->setValue(value);
// moveToMostRecent(p);
//}
//
////将结点移动到尾部
//template<typename Key, typename Value>
//void Cache::LruCache<Key,Value>::moveToMostRecent(NodePtr p)
//{
// node n= *p;
// deleteNode(p);
// NodePtr newP=std::make_shared<n>();
// insertList(newP);
//}
//
////删除结点
//template<typename Key, typename Value>
//void Cache::LruCache<Key,Value>::deleteNode(NodePtr p)//仅仅是从链表中移除
//{
// if (p == headPtr || p == tailPtr)return;//不能删除头节点或者尾结点
// auto it = p->pre.lock();//获得上个结点的share_ptr
// auto it2 = p->next;
// it->next = it2;
// it2->pre = it;
// p.next = nullptr;//清空p的next,使得p与链表彻底断开
//}
//
////将节点插入到链表当中(尾插法)
//template<typename Key,typename Value>
//void Cache::LruCache<Key,Value>::insertList(NodePtr p)
//{
// auto it = tailPtr->pre.lock();
// it->next = p;
// p->pre = it;
//}
//
////清理掉最久没有访问的结点
//template<typename Key, typename Value>
//void Cache::LruCache<Key,Value>::evicLeastRecent()
//{
// auto it = headPtr->next;
// if (it == tailPtr)return;//不能删除尾结点,但实际上这行代码可以省略,因为调用这个的时候会先检查capacity是否为0
// Key k = it.getKey();
// headPtr->next = it;
// it->next->pre = headPtr;
// deleteNode(it);
// this->valueMap.erase(k);
//}