{"action":"opened","issue":{"url":"https://api.github.com/repos/cayasso/cacheman/issues/8","labels_url":"https://api.github.com/repos/cayasso/cacheman/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/cayasso/cacheman/issues/8/comments","events_url":"https://api.github.com/repos/cayasso/cacheman/issues/8/events","html_url":"https://github.com/cayasso/cacheman/issues/8","id":70522519,"number":8,"title":"Cache with fuzzy logic","user":{"login":"markotom","id":914160,"avatar_url":"https://avatars.githubusercontent.com/u/914160?v=3","gravatar_id":"","url":"https://api.github.com/users/markotom","html_url":"https://github.com/markotom","followers_url":"https://api.github.com/users/markotom/followers","following_url":"https://api.github.com/users/markotom/following{/other_user}","gists_url":"https://api.github.com/users/markotom/gists{/gist_id}","starred_url":"https://api.github.com/users/markotom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markotom/subscriptions","organizations_url":"https://api.github.com/users/markotom/orgs","repos_url":"https://api.github.com/users/markotom/repos","events_url":"https://api.github.com/users/markotom/events{/privacy}","received_events_url":"https://api.github.com/users/markotom/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-04-23T21:52:14Z","updated_at":"2015-04-23T21:52:14Z","closed_at":null,"body":"Hi everyone!\r\n\r\nCurrently I'm building a wrapper based on Cacheman that supports fuzzy logic to make a flush of data. I don't know if it could be part of a new feature of Cacheman. I did an example to accomplish that, but would be nice to have in this library.\r\n\r\n```js\r\nvar Cacheman = require('cacheman');\r\nvar fuzzylogic = require('fuzzylogic');\r\n\r\n// Set more info to make fuzzy logic\r\n(function (set) {\r\n    Cacheman.prototype.set = function (key, data) {\r\n      var info = {\r\n        key: key,\r\n        cached_time: Date.now(),\r\n        expiration_time: Date.now() + (this._ttl * 1000)\r\n      };\r\n\r\n      // Send info to Redis\r\n      this._engine.set(this.key(key) + ':info', info, 86400);\r\n\r\n      return set.apply(this, arguments);\r\n    };\r\n})(Cacheman.prototype.set);\r\n\r\n// Fuzzy logic\r\n(function (get) {\r\n    Cacheman.prototype.get = function (key) {\r\n      this._engine.get(this.key(key) + ':info', function (err, info) {\r\n        if (info) {\r\n          var timelapse = this._ttl * 1000;\r\n\r\n          var half = timelapse / 2;\r\n          var qrt = half / 2;\r\n          var mid_time = info.cached_time + half + qrt;\r\n\r\n          var request_time = Date.now();\r\n\r\n          var fuzzy = fuzzylogic.trapezoid(request_time, info.cached_time, info.cached_time, mid_time, info.expiration_time);\r\n          var valid = !!Math.round(fuzzy);\r\n\r\n          if (!valid) {\r\n            console.log('Flushed!');\r\n            this.del(key);\r\n            this._engine.del(this.key(key) + ':info')\r\n          } else {\r\n            console.log(fuzzy, valid);\r\n          }\r\n        }\r\n      }.bind(this));\r\n\r\n      return get.apply(this, arguments);\r\n    };\r\n})(Cacheman.prototype.get);\r\n\r\n\r\n/* Example */\r\n\r\nvar cacheHandler = new Cacheman('test', {\r\n  ttl: 10, // 10 seconds\r\n  engine: 'redis',\r\n  port: 6379,\r\n  host: 'localhost'\r\n});\r\n\r\nvar testCache = function () {\r\n  cacheHandler.get('mykey', function (err, value) {\r\n    if (!value) {\r\n      cacheHandler.set('mykey', { foo: 'bar' }, function (err, value) {\r\n        console.log(value);\r\n      });\r\n    }\r\n  });\r\n};\r\n\r\nsetInterval(testCache, 250);\r\ntestCache();\r\n```\r\n\r\nWhat do you think?\r\n"}}