PageRenderTime 39ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cache.vala

http://github.com/boredomist/valatra
Vala | 68 lines | 49 code | 19 blank | 0 comment | 1 complexity | 735bbd8fd8b36159b0bd24a743e17f5a MD5 | raw file
 1using Gee;
 2
 3namespace Valatra {
 4
 5  public class CacheEntry : GLib.Object {
 6    private Checksum checksum;
 7
 8    public int size;
 9    public string content;
10    public string etag;
11    public DateTime modified;
12
13    public CacheEntry(string cont, int sz = -1) {
14      checksum = new Checksum(ChecksumType.MD5);
15
16      content = cont;
17      modified = new DateTime.now_utc();
18
19      size = sz;
20
21      update_etag();
22    }
23
24    public void update(string cont, int sz = -1) {
25      content = cont;
26      size = sz;
27
28      modified = new DateTime.now_utc();
29
30      update_etag();
31    }
32
33    public void update_etag() {
34      checksum.update((uchar[])content, size);
35
36      etag = checksum.get_string().dup();
37      checksum = new Checksum(ChecksumType.SHA1);
38    }
39  }
40
41  public class Cache : GLib.Object {
42    private HashMap<string, CacheEntry> entries;
43
44    public Cache() {
45      entries  = new HashMap<string, CacheEntry>();
46    }
47
48    public void add(string path, string content) {
49      var ent = new CacheEntry(content);
50      this.set(path, ent);
51    }
52
53    public new void set(string path, CacheEntry entry) {
54      entries.set(path, entry);
55    }
56
57    public new CacheEntry? get(string path) {
58      return entries.get(path);
59    }
60
61    public void invalidate(string path) {
62      if(entries.has_key(path)) {
63        entries.unset(path);
64      }
65    }
66  }
67}
68