Mikael's blog

A developers sixth time trying to maintain a blog

Source Code - Root / cacheManager.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*****************************************
 *   Cache Manager
 *****************************************
 *   Author:  mikael.lofjard@gmail.com
 *   Website: http://lofjard.se
 *   License: MIT License
 ****************************************/
 
var CacheManager = (function () {
 
  var env = require('./environmentManager').EnvironmentManager;
  var searchManager = require('./searchManager').SearchManager;
  var misc = require('./misc').Misc;
 
  var cacheStore = {};
   
  function getStoreType(url)
  {
    var urlParts = url.split('/');
    var result = 'dynamic';
 
    switch (urlParts[1]) {
      case 'search':
        result = 'none';
        break;
      case 'source':
      case 'about':
        result = 'static';
        break;
      case 'archive':
      case 'atomfeed':
/*        result = 'semiStatic';
        break;*/
      case 'tags':
      case 'tag':
        result = 'semiDynamic';
        break;
      case 'post':
        result = 'floating';
        break;
    }
 
    return result;
  }
 
  return {
 
    init: function () {
      cacheStore = {};
      cacheStore.static = {};       // static between boots       - /source /about
      cacheStore.semiStatic = {};   // clear on new post          - /archive /atomfeed
      cacheStore.semiDynamic = {};  // clear on edit post         - /tags /tag
      cacheStore.dynamic = {};    // clear on comment (default) - / /page
      cacheStore.floating = {};   // null item on comment       - /post
    },
 
    clearOnNewPost: function () {
      env.info('CacheManager: Clearing cache on new post');
      cacheStore.semiStatic = {};
      cacheStore.semiDynamic = {};
      cacheStore.dynamic = {};
      searchManager.rescan();
    },
 
    clearOnEditPost: function (url) {
      env.info('CacheManager: Clearing cache on edit for ' + url);
      cacheStore.semiDynamic = {};
      cacheStore.dynamic = {};
 
      delete(cacheStore[getStoreType(url)][url]);
      searchManager.rescan();
    },
 
    clearOnNewComment: function (url) {
      env.info('CacheManager: Clearing cache on comment for ' + url);
      cacheStore.dynamic = {};
      delete(cacheStore[getStoreType(url)][url]);
    },
 
    cache: function (url, headerData, contentData) {
      var storeType = getStoreType(url);
      if (storeType != 'none') {
        env.info('CacheManager: Caching content for ' + url);
        cacheStore[storeType][url] = { content: contentData, headers: headerData };
      }
    },
 
    fetch: function (url) {
      var storeType = getStoreType(url);
      if (storeType != 'none') {
        var data = cacheStore[getStoreType(url)][url];
 
        if (typeof(data) != 'undefined') {
          env.info('CacheManager: Found cached entry for ' + url);
          return data;
        }
      }
 
      return null;
    }
 
  };
 
}());
 
typeof(exports) != 'undefined' ? exports.CacheManager = CacheManager : null;