BVB Source Codes

parse-server Show InMemoryCache.js Source code

Return Download parse-server: download InMemoryCache.js Source code - Download parse-server Source code - Type:.js
  1. const DEFAULT_CACHE_TTL = 5 * 1000;
  2.  
  3.  
  4. export class InMemoryCache {
  5.   constructor({
  6.     ttl = DEFAULT_CACHE_TTL
  7.   }) {
  8.     this.ttl = ttl;
  9.     this.cache = Object.create(null);
  10.   }
  11.  
  12.   get(key) {
  13.     const record = this.cache[key];
  14.     if (record == null) {
  15.       return null;
  16.     }
  17.  
  18.     // Has Record and isnt expired
  19.     if (isNaN(record.expire) || record.expire >= Date.now()) {
  20.       return record.value;
  21.     }
  22.  
  23.     // Record has expired
  24.     delete this.cache[key];
  25.     return null;
  26.   }
  27.  
  28.   put(key, value, ttl = this.ttl) {
  29.     if (ttl < 0 || isNaN(ttl)) {
  30.       ttl = NaN;
  31.     }
  32.  
  33.     var record = {
  34.       value: value,
  35.       expire: ttl + Date.now()
  36.     }
  37.  
  38.     if (!isNaN(record.expire)) {
  39.       record.timeout = setTimeout(() => {
  40.         this.del(key);
  41.       }, ttl);
  42.     }
  43.  
  44.     this.cache[key] = record;
  45.   }
  46.  
  47.   del(key) {
  48.     var record = this.cache[key];
  49.     if (record == null) {
  50.       return;
  51.     }
  52.  
  53.     if (record.timeout) {
  54.       clearTimeout(record.timeout);
  55.     }
  56.     delete this.cache[key];
  57.   }
  58.  
  59.   clear() {
  60.     this.cache = Object.create(null);
  61.   }
  62.  
  63. }
  64.  
  65. export default InMemoryCache;
  66.  
downloadInMemoryCache.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top