Skip to content

Commit c6a24ca

Browse files
committed
Implemented a simple caching mechanism in RAM.
Feed the watchdog so it doesn't die while iterating over cards.
1 parent a0311ee commit c6a24ca

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/ACNodeClient.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ACNodeClient
3232
#include "card.h"
3333
#include "sdcache.h"
3434
#include "eepromcache.h"
35-
#include "nocache.h"
35+
#include "ramcache.h"
3636
#include "doorbot.h"
3737
#include "doorbot_ac.h"
3838

@@ -109,8 +109,8 @@ void setup() {
109109
Serial.println("Using the SD Card to cache cards");
110110
} else {
111111
Serial.println("SD card could not be accessed");
112-
Serial.println("Please fix the SD card and try again. Cache is disabled until reboot.");
113-
cache = new NoCache();
112+
Serial.println("Please fix the SD card and try again. Caching cards in RAM for now.");
113+
cache = new RAMCache();
114114
}
115115
} else {
116116
Serial.println("Using the eeprom to cache cards");

src/cache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void Cache::fill(const int count) {
2121
}
2222

2323
void list_callback(Card c) {
24+
wdog.feed();
2425
c.dump();
2526
}
2627

@@ -30,4 +31,3 @@ void Cache::list(void) {
3031
Serial.print(count);
3132
Serial.println(" users");
3233
}
33-

src/ramcache.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "ramcache.h"
22
#include "network.h"
3+
#include "watchdog.h"
4+
5+
extern Watchdog wdog;
36

47
CacheEntry::CacheEntry() :
58
expires_at(0),
@@ -26,6 +29,8 @@ void CacheEntry::expire() {
2629
}
2730

2831
void RAMCache::begin() {
32+
Serial.println("RAM Cache TTL: " + String(CACHE_TTL));
33+
Serial.println("RAM Cache memory: " + String(sizeof(CacheEntry) * CACHE_CAPACITY));
2934
purge();
3035
}
3136

@@ -88,6 +93,7 @@ void RAMCache::verify() {
8893
for (int i = 0; i < CACHE_CAPACITY; i++) {
8994
CacheEntry *entry = &entries[i];
9095
if (!entry->expired(now)) {
96+
wdog.feed();
9197
int status = networking::querycard(entry->card);
9298
switch (status) {
9399
case 2:

src/ramcache.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef _RAMCACHE_H
2+
#define _RAMCACHE_H
3+
4+
#include "cache.h"
5+
#include "card.h"
6+
7+
#define CACHE_TTL 8 * 3600 * 1000 // eight hours
8+
#define CACHE_CAPACITY 1500
9+
10+
class CacheEntry {
11+
public:
12+
CacheEntry();
13+
CacheEntry(Card c);
14+
bool expired(unsigned long now);
15+
void expire();
16+
void touch(unsigned long now);
17+
unsigned long expires_at;
18+
Card card;
19+
};
20+
21+
class RAMCache : public Cache {
22+
public:
23+
virtual void begin();
24+
virtual Card get(Card u);
25+
virtual void set(const Card u);
26+
virtual void purge(void);
27+
virtual int each(void( *callback)(Card c));
28+
virtual void verify(void);
29+
private:
30+
CacheEntry entries[CACHE_CAPACITY];
31+
};
32+
33+
#endif

0 commit comments

Comments
 (0)