-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make some small fixed and using preclare
- Loading branch information
Showing
16 changed files
with
267 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#include "LFUCache.h" | ||
#include "MutexLock.h" | ||
#include "Conf.h" | ||
#include "MemoryPool.h" | ||
|
||
void KeyList::init(int fq) | ||
{ | ||
freq=fq; | ||
//head=last=new Node<Key>; | ||
head=last=newElement<Node<Key>>(); | ||
head->setNext(NULL); | ||
} | ||
|
||
void KeyList::destory(){ | ||
while(head){ | ||
key_node pre=head; | ||
head=head->getNext(); | ||
//delete pre; | ||
deleteElement(pre); | ||
} | ||
} | ||
|
||
int KeyList::getFreq(){ | ||
return freq; | ||
} | ||
|
||
void KeyList::add(key_node &node){ | ||
if(head->getNext()){ | ||
head->getNext()->setPre(node); | ||
//printf("is not one\n"); | ||
} | ||
else | ||
last=node; | ||
node->setNext(head->getNext()); | ||
node->setPre(head); | ||
head->setNext(node); | ||
//printf("last key=%d\n",last->getValue().key); | ||
} | ||
|
||
void KeyList::del(key_node &node){ | ||
node->getPre()->setNext(node->getNext()); | ||
if(node->getNext()) | ||
node->getNext()->setPre(node->getPre()); | ||
else | ||
last=node->getPre(); | ||
} | ||
|
||
bool KeyList::isEmpty(){ | ||
return head==last; | ||
} | ||
|
||
key_node KeyList::getLast(){ | ||
return last; | ||
} | ||
|
||
void LFUCache::init(){ | ||
capacity=getconf().getcapacity(); | ||
//head=new Node<KeyList>; | ||
head=newElement<Node<KeyList>>(); | ||
head->getValue().init(0); | ||
head->setNext(NULL); | ||
} | ||
|
||
LFUCache::~LFUCache(){ | ||
while(head){ | ||
freq_node pre=head; | ||
head=head->getNext(); | ||
pre->getValue().destory(); | ||
//delete pre; | ||
deleteElement(pre); | ||
} | ||
} | ||
|
||
void LFUCache::addfreq(key_node &nowk,freq_node &nowf){ | ||
freq_node nxt; | ||
if(!nowf->getNext()||nowf->getNext()->getValue().getFreq()!=nowf->getValue().getFreq()+1){ | ||
//插入freqnode | ||
//printf("new freqnode!\n"); | ||
//nxt=new Node<KeyList>; | ||
nxt=newElement<Node<KeyList>>(); | ||
nxt->getValue().init(nowf->getValue().getFreq()+1); | ||
if(nowf->getNext()) | ||
nowf->getNext()->setPre(nxt); | ||
nxt->setNext(nowf->getNext()); | ||
nowf->setNext(nxt); | ||
nxt->setPre(nowf); | ||
} | ||
else | ||
nxt=nowf->getNext(); | ||
fmap[nowk->getValue().key]=nxt; | ||
//移动keynode | ||
if(nowf!=head){ | ||
nowf->getValue().del(nowk); | ||
//printf("nowf is not head!\n"); | ||
} | ||
nxt->getValue().add(nowk); | ||
if(nowf!=head&&nowf->getValue().isEmpty()) | ||
del(nowf); | ||
} | ||
|
||
bool LFUCache::get(string &key,string &v){ | ||
if(!capacity) | ||
return false; | ||
MutexLockGuard lock(mutex); | ||
if(fmap.find(key)!=fmap.end()){ | ||
//命中 | ||
key_node nowk=kmap[key]; | ||
freq_node nowf=fmap[key]; | ||
v+=nowk->getValue().value; | ||
addfreq(nowk,nowf); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
void LFUCache::set(string &key,string &v){ | ||
if(!capacity) | ||
return; | ||
//printf("kmapsize=%d capacity=%d\n",kmap.size(),capacity); | ||
MutexLockGuard lock(mutex); | ||
if(kmap.size()==capacity){ | ||
freq_node headnxt=head->getNext(); | ||
key_node last=headnxt->getValue().getLast(); | ||
headnxt->getValue().del(last); | ||
//printf("key=%d\n",last->getValue().key); | ||
kmap.erase(last->getValue().key); | ||
fmap.erase(last->getValue().key); | ||
//delete last; | ||
deleteElement(last); | ||
if(headnxt->getValue().isEmpty()) | ||
del(headnxt); | ||
} | ||
//key_node nowk=new Node<Key>; | ||
key_node nowk=newElement<Node<Key>>(); | ||
nowk->getValue().key=key; | ||
nowk->getValue().value=v; | ||
addfreq(nowk,head); | ||
kmap[key]=nowk; | ||
} | ||
|
||
void LFUCache::del(freq_node &node){ | ||
node->getPre()->setNext(node->getNext()); | ||
if(node->getNext()) | ||
node->getNext()->setPre(node->getPre()); | ||
node->getValue().destory(); | ||
//delete node; | ||
deleteElement(node); | ||
} | ||
|
||
LFUCache& getCache(){ | ||
static LFUCache cache; | ||
return cache; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
|
||
using std::string; | ||
|
||
class MutexLock; | ||
|
||
template<typename T> | ||
class Node{ | ||
private: | ||
T value; | ||
Node* pre; | ||
Node* next; | ||
|
||
public: | ||
void setPre(Node *p){ | ||
pre=p; | ||
} | ||
void setNext(Node *n){ | ||
next=n; | ||
} | ||
Node* getPre(){ | ||
return pre; | ||
} | ||
Node* getNext(){ | ||
return next; | ||
} | ||
T& getValue(){ | ||
return value; | ||
} | ||
|
||
}; | ||
|
||
struct Key{ | ||
string key,value; | ||
}; | ||
|
||
typedef Node<Key>* key_node; | ||
|
||
class KeyList{ | ||
private: | ||
int freq; | ||
key_node head; | ||
key_node last; | ||
|
||
public: | ||
void init(int fq); | ||
void destory(); | ||
int getFreq(); | ||
void add(key_node &node); | ||
void del(key_node &node); | ||
bool isEmpty(); | ||
key_node getLast(); | ||
}; | ||
|
||
typedef Node<KeyList>* freq_node; | ||
|
||
class LFUCache{ | ||
private: | ||
freq_node head; | ||
int capacity; | ||
MutexLock mutex; | ||
|
||
std::unordered_map<string,key_node> kmap; //key到keynode的映射 | ||
std::unordered_map<string,freq_node> fmap; //key到freqnode的映射 | ||
|
||
void addfreq(key_node &nowk,freq_node &nowf); | ||
void del(freq_node &node); | ||
|
||
public: | ||
void init(); | ||
~LFUCache(); | ||
bool get(string &key,string &value); //通过key返回value并进行LFU操作 | ||
void set(string &key,string &value); //更新LFU缓存 | ||
}; | ||
|
||
LFUCache& getCache(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#pragma once |