aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cache.h25
-rw-r--r--src/cachecalc.h4
-rw-r--r--src/tests/cache.cpp17
3 files changed, 38 insertions, 8 deletions
diff --git a/src/cache.h b/src/cache.h
index 036ceb4..4ae1aa8 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -143,6 +143,7 @@ namespace Bu
143 { 143 {
144 obtype *pData; 144 obtype *pData;
145 int iRefs; 145 int iRefs;
146 time_t tLastSync;
146 } CacheEntry; 147 } CacheEntry;
147 148
148 typedef Bu::Hash<keytype, CacheEntry> CidHash; 149 typedef Bu::Hash<keytype, CacheEntry> CidHash;
@@ -184,7 +185,7 @@ namespace Bu
184 Ptr insert( obtype *pData ) 185 Ptr insert( obtype *pData )
185 { 186 {
186 TRACE( pData ); 187 TRACE( pData );
187 CacheEntry e = {pData, 0}; 188 CacheEntry e = {pData, 0, 0};
188 keytype k = pStore->create( pData ); 189 keytype k = pStore->create( pData );
189 hEnt.insert( k, e ); 190 hEnt.insert( k, e );
190 191
@@ -205,7 +206,7 @@ namespace Bu
205 return Ptr( this, hEnt.get( cId ).pData, cId ); 206 return Ptr( this, hEnt.get( cId ).pData, cId );
206 } 207 }
207 catch( Bu::HashException &e ) { 208 catch( Bu::HashException &e ) {
208 CacheEntry e = {pStore->load( cId ), 0}; 209 CacheEntry e = {pStore->load( cId ), 0, time( NULL )};
209 pCalc->onLoad( e.pData, cId ); 210 pCalc->onLoad( e.pData, cId );
210 hEnt.insert( cId, e ); 211 hEnt.insert( cId, e );
211 return Ptr( this, e.pData, cId ); 212 return Ptr( this, e.pData, cId );
@@ -285,11 +286,19 @@ namespace Bu
285 { 286 {
286 if( i.getValue().iRefs == 0 ) 287 if( i.getValue().iRefs == 0 )
287 { 288 {
288 pStore->sync( 289 if( pCalc->shouldSync(
289 i.getValue().pData, 290 i.getValue().pData,
290 i.getKey() 291 i.getKey(),
291 ); 292 i.getValue().tLastSync
292 iSynced++; 293 ) )
294 {
295 pStore->sync(
296 i.getValue().pData,
297 i.getKey()
298 );
299 iSynced++;
300 i.getValue().tLastSync = time( NULL );
301 }
293 } 302 }
294 } 303 }
295 if( iSynced > 0 ) 304 if( iSynced > 0 )
@@ -319,7 +328,7 @@ namespace Bu
319 return hEnt.get( cId ).pData; 328 return hEnt.get( cId ).pData;
320 } 329 }
321 catch( Bu::HashException &e ) { 330 catch( Bu::HashException &e ) {
322 CacheEntry e = {pStore->load( cId ), 0}; 331 CacheEntry e = {pStore->load( cId ), 0, time( NULL )};
323 pCalc->onLoad( e.pData, cId ); 332 pCalc->onLoad( e.pData, cId );
324 hEnt.insert( cId, e ); 333 hEnt.insert( cId, e );
325 return e.pData; 334 return e.pData;
diff --git a/src/cachecalc.h b/src/cachecalc.h
index dd9712f..7fdb16e 100644
--- a/src/cachecalc.h
+++ b/src/cachecalc.h
@@ -3,6 +3,8 @@
3 3
4#include "bu/trace.h" 4#include "bu/trace.h"
5 5
6#include <time.h>
7
6namespace Bu 8namespace Bu
7{ 9{
8 template<class obtype, class keytype> class Cache; 10 template<class obtype, class keytype> class Cache;
@@ -27,6 +29,8 @@ namespace Bu
27 29
28 virtual void onLoad( obtype *pSrc, const keytype &key )=0; 30 virtual void onLoad( obtype *pSrc, const keytype &key )=0;
29 virtual void onUnload( obtype *pSrc, const keytype &key )=0; 31 virtual void onUnload( obtype *pSrc, const keytype &key )=0;
32 virtual bool shouldSync( obtype *pSrc, const keytype &key,
33 time_t tLastSync )=0;
30 virtual void onTick() { }; 34 virtual void onTick() { };
31 35
32 protected: 36 protected:
diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp
index 323edda..8d95a9d 100644
--- a/src/tests/cache.cpp
+++ b/src/tests/cache.cpp
@@ -96,6 +96,19 @@ public:
96 f.write( s ); 96 f.write( s );
97 } 97 }
98 98
99 virtual void sync( Bob *, const long & )
100 {
101 }
102
103 virtual void sync()
104 {
105 }
106
107 virtual bool has( const long & )
108 {
109 return true;
110 }
111
99 virtual Bob *load( const long &key ) 112 virtual Bob *load( const long &key )
100 { 113 {
101 TRACE( key ); 114 TRACE( key );
@@ -156,6 +169,10 @@ public:
156 { 169 {
157 } 170 }
158 171
172 virtual bool shouldSync( Bob *, const long &, time_t )
173 {
174 }
175
159private: 176private:
160 177
161}; 178};