-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheStore.h
106 lines (80 loc) · 2.66 KB
/
cacheStore.h
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
//
// Created by Ruwan on 10/20/2017.
//
#include <iostream>
#include <thread>
/*
#ifndef CACHEENGINE_CACHESTORE_H
#define CACHEENGINE_CACHESTORE_H
#endif //CACHEENGINE_CACHESTORE_H*/
using namespace std;
namespace CacheStore {
int nextAccessIndex = 0;
int BLOCK_SIZE = 5;
struct Block{
int ref;
int attempt;
string value;
};
struct CacheRow {
private:
int nextAccessIndex;
public:
Block *cache;
int ROW_SIZE;
void init(int rowSize) {
this->cache = new Block[rowSize];
this->ROW_SIZE = rowSize;
this->nextAccessIndex = 0;
for (int i=0; i < rowSize; i++) {
this->cache[i].ref = 0;
this->cache[i].attempt = 0;
this->cache[i].value = "";
}
};
int addBlock (Block block) {
int hasIndex = this->hasHadata(block.ref);
switch (hasIndex){
case -1:
this->cache[this->nextAccessIndex] = block;
this->nextAccessIndex++;
cout << "sleep" << endl;
Sleep(1000);
cout << "added" << endl;
break;
default:
this->cache[hasIndex].attempt++;
this->cache[hasIndex].value = block.value;
break;
}
if(this->nextAccessIndex == this->ROW_SIZE) this->nextAccessIndex = 0;
return this->nextAccessIndex - 1;
}
void removeBlock (int ref) {
for(int i =0; i < this->ROW_SIZE; i++){
if(this->cache[i].ref == ref) {
for (int j = i; j < (this->ROW_SIZE-1); j++ ) {
Block temp = cache[j];
cache[j] = cache[j+1];
cache[j+1] = temp;
}
Block empty;
empty.ref = 0;
empty.attempt = 0;
empty.value = "";
this->cache[this->ROW_SIZE-1] = empty;
this->nextAccessIndex = this->ROW_SIZE-2;
cout << "removed" << endl;
}
}
}
int hasHadata(int ref){
for(int i =0; i < this->ROW_SIZE; i++)
if(this->cache[i].ref == ref) return i;
return -1;
}
void viewAll(){
for(int i =0; i < this->ROW_SIZE; i++) if(this->cache[i].ref) cout << this->cache[i].ref << "\t\t" << this->cache[i].attempt << "\t" << this->cache[i].value << endl;
}
};
}