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
  
//! An LRU, size-constrained expiration policy manager. 
//! 
//! @thanks 
//!   Thanks to Francesco Chemolli <kinkie@@roxen.com> for the contribution. 
 
#pike __REAL_VERSION__ 
 
#if defined(CACHE_DEBUG)||defined(CACHE_POLICY_DEBUG)||defined(CACHE_POLICY_SIZED_DEBUG) 
#define CACHE_WERR(X...) werror("Cache.Policy.Sized: "+X) 
#else 
#define CACHE_WERR(X...) 
#endif /* CACHE_DEBUG || CACHE_POLICY_DEBUG || CACHE_POLICY_SIZED_DEBUG */ 
 
inherit Cache.Policy.Base; 
//watermarks 
int max_size=0; //in bytes 
int min_size=0; 
 
//used for the "candidate-for-removal" array 
#define KEY 0 
#define SIZE 1 
 
//! 
void expire (Cache.Storage.Base storage) { 
  ADT.Priority_queue removables=ADT.Priority_queue(); 
  Cache.Data got; 
  mixed tmp; 
  int now=time(1); 
  int current_size=0; //in bytes. Should I use kb maybe? 
 
  CACHE_WERR("expiring cache\n"); 
  string key=storage->first(); 
  while (key) { 
    got=storage->get(key,1); 
    CACHE_WERR("examining: %s (age: %d, size: %d). Current size is %d\n", 
               key,now-(got->atime), got->size(), current_size); 
    if (tmp=(got->etime) && tmp < now) { //explicit expiration 
      CACHE_WERR("expired\n"); 
      storage->delete(key); 
      key=storage->next(); 
      continue; 
    } 
    current_size+=got->size(); 
    removables->push( got->atime, ({ key, got->size() })  ); 
    if (current_size > max_size) { 
      array candidate; 
      while (current_size > min_size) { 
        candidate=removables->pop(); 
        CACHE_WERR("deleting %s (size: %d)\n",candidate[KEY],candidate[SIZE]); 
        storage->delete(candidate[KEY]); 
        current_size-=candidate[SIZE]; 
      } 
    } 
    key=storage->next(); 
  } 
} 
 
//! 
void create (int max, void|int min) { 
  max_size=max; 
  if (min) 
    min_size=min; 
  else if (undefinedp(min)) //not specified 
    min_size=max_size/2; 
}