This repository was archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.h
More file actions
88 lines (69 loc) · 1.82 KB
/
Copy pathCache.h
File metadata and controls
88 lines (69 loc) · 1.82 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
//
// Created by X G and HP on 2/17/22.
//
#ifndef HTTP_PROXY_SERVER_CACHE_H
#define HTTP_PROXY_SERVER_CACHE_H
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
// Def the double linked node
struct ListNode{
std::string key; // Define the key for the url
std::pair<std::vector<char>, time_t> response;
ListNode * prev;
ListNode * next;
ListNode():key(""), response({}),prev(nullptr), next(nullptr){} // default constructor
ListNode(string key, std::pair<std::vector<char>, time_t> response): key(key), response(response), prev(nullptr), next(nullptr){}
};
/**
* Define the LRU Cache
*/
class Cache {
private:
unordered_map<string,ListNode*> cache;
ListNode* head;
ListNode* tail;
int size;
int capacity;
protected:
/**
* Move the curr node to the head
* @param node to be move to the head
*/
void move2Head(ListNode* node);
/**
* remove the tail of ll and return the removed node
* @return the node to be removed
*/
ListNode *rmTail();
/**
* Add the node into the head of ll
* @param pNode to be added
*/
void add2Head(ListNode *pNode);
public:
/**
* The constructor of the LRU cache with size 0
* @param my_capacity of cache{
*/
Cache(int my_capacity):capacity(my_capacity),size(0){
head = new ListNode();
tail=new ListNode();
head->next=tail;
tail->prev=head;
}
/**
* Get the key
* @param key of the url
* @return {} when not found, or return response
*/
std::pair<std::vector<char>, time_t> get(string key);
/**
* Put the key into the Cache
* @param key url
* @param response
*/
void put(string key,std::pair<std::vector<char>, time_t> response);
};
#endif //HTTP_PROXY_SERVER_CACHE_H