/src/cache.vala

http://github.com/boredomist/valatra · Vala · 68 lines · 49 code · 19 blank · 0 comment · 1 complexity · 735bbd8fd8b36159b0bd24a743e17f5a MD5 · raw file

  1. using Gee;
  2. namespace Valatra {
  3. public class CacheEntry : GLib.Object {
  4. private Checksum checksum;
  5. public int size;
  6. public string content;
  7. public string etag;
  8. public DateTime modified;
  9. public CacheEntry(string cont, int sz = -1) {
  10. checksum = new Checksum(ChecksumType.MD5);
  11. content = cont;
  12. modified = new DateTime.now_utc();
  13. size = sz;
  14. update_etag();
  15. }
  16. public void update(string cont, int sz = -1) {
  17. content = cont;
  18. size = sz;
  19. modified = new DateTime.now_utc();
  20. update_etag();
  21. }
  22. public void update_etag() {
  23. checksum.update((uchar[])content, size);
  24. etag = checksum.get_string().dup();
  25. checksum = new Checksum(ChecksumType.SHA1);
  26. }
  27. }
  28. public class Cache : GLib.Object {
  29. private HashMap<string, CacheEntry> entries;
  30. public Cache() {
  31. entries = new HashMap<string, CacheEntry>();
  32. }
  33. public void add(string path, string content) {
  34. var ent = new CacheEntry(content);
  35. this.set(path, ent);
  36. }
  37. public new void set(string path, CacheEntry entry) {
  38. entries.set(path, entry);
  39. }
  40. public new CacheEntry? get(string path) {
  41. return entries.get(path);
  42. }
  43. public void invalidate(string path) {
  44. if(entries.has_key(path)) {
  45. entries.unset(path);
  46. }
  47. }
  48. }
  49. }