1 ///
2 module cachetools.interfaces;
3 
4 import std.typecons;
5 import std.datetime;
6 import std.typecons;
7 
8 private import cachetools.internal;
9 
10 //
11 // cache have aspects:
12 // 1. storage: hashmap and some kind of order of elements
13 // 2. stream of evicted elements, which user may want to handle(slose files, sockets, etc)
14 // 3. eviction policy (condition to start/stop evinction)
15 //
16 
17 ///
18 enum PutResultFlag
19 {
20     None,
21     Inserted = 1 << 0,
22     Replaced = 1 << 1,
23     Evicted  = 1 << 2
24 }
25 ///
26 alias PutResult = BitFlags!PutResultFlag;
27 
28 // I failed to reach both goals: inheritance from interface and nogc/nothrow attribute neutrality
29 // for Cache implementations. So I droped inheritance.
30 //
31 //interface Cache(K, V) {
32 //
33 //    // get value from cache
34 //    Nullable!V get(K) @safe;
35 //
36 //    // put/update cache entry
37 //    PutResult put(K, V) @safe;
38 //
39 //    // remove key
40 //    bool  remove(K) @safe;
41 //    
42 //    // clear entire cache
43 //    void  clear() @safe;
44 //    
45 //    // # of elements
46 //    size_t length() const @safe;
47 //
48 //}
49 
50 enum EventType
51 {
52     Removed,
53     Expired,
54     Evicted,
55     Updated
56 }
57 
58 struct CacheEvent(K, V)
59 {
60     EventType       event;
61     StoredType!K    key;
62     StoredType!V    val;
63 }
64 
65 /**
66  * TTL encapsulate ttl for single cache item
67     1. use default - __ttl = 0
68     2. no ttl      - __ttl = -1
69     3. some value  - __ttl > 0
70  */
71 struct TTL {
72     import core.stdc.time;
73 
74     private time_t  __ttl = 0;
75 
76     ///
77     /// True if this TTL means - use default value for this cache
78     ///
79     bool useDefault() pure const nothrow @nogc @safe {
80         return __ttl == 0;
81     }
82     ///
83     /// return value encapsulated by this ttl
84     ///
85     time_t value() pure const nothrow @nogc @safe {
86         return __ttl;
87     }
88     ///
89     /// Create "no ttl" - means do not use ttl with this entry
90     ///
91     TTL opUnary(string op)() pure nothrow @safe @nogc if (op == "~")
92     {
93         return TTL(-1);
94     }
95     /**
96     / Constructor
97     / Parameters:
98     / v - ttl value (0 - use default value or no ttl if there is no defaults)
99     */
100     this(int v) pure nothrow @safe @nogc {
101         if ( v < 0 ) {
102             __ttl = -1;
103         }
104         else
105         {
106             __ttl = v;
107         }
108     }
109 }