BVB Source Codes

parse-server Show SchemaCache.js Source code

Return Download parse-server: download SchemaCache.js Source code - Download parse-server Source code - Type:.js
  1. const MAIN_SCHEMA = "__MAIN_SCHEMA";
  2. const SCHEMA_CACHE_PREFIX = "__SCHEMA";
  3. const ALL_KEYS = "__ALL_KEYS";
  4.  
  5. import { randomString } from '../cryptoUtils';
  6. import defaults from '../defaults';
  7.  
  8. export default class SchemaCache {
  9.   cache: Object;
  10.  
  11.   constructor(cacheController, ttl = defaults.schemaCacheTTL, singleCache = false) {
  12.     this.ttl = ttl;
  13.     if (typeof ttl == 'string') {
  14.       this.ttl = parseInt(ttl);
  15.     }
  16.     this.cache = cacheController;
  17.     this.prefix = SCHEMA_CACHE_PREFIX;
  18.     if (!singleCache) {
  19.       this.prefix += randomString(20);
  20.     }
  21.   }
  22.  
  23.   put(key, value) {
  24.     return this.cache.get(this.prefix + ALL_KEYS).then((allKeys) => {
  25.       allKeys = allKeys || {};
  26.       allKeys[key] = true;
  27.       return Promise.all([this.cache.put(this.prefix + ALL_KEYS, allKeys, this.ttl), this.cache.put(key, value, this.ttl)]);
  28.     });
  29.   }
  30.  
  31.   getAllClasses() {
  32.     if (!this.ttl) {
  33.       return Promise.resolve(null);
  34.     }
  35.     return this.cache.get(this.prefix + MAIN_SCHEMA);
  36.   }
  37.  
  38.   setAllClasses(schema) {
  39.     if (!this.ttl) {
  40.       return Promise.resolve(null);
  41.     }
  42.     return this.put(this.prefix + MAIN_SCHEMA, schema);
  43.   }
  44.  
  45.   setOneSchema(className, schema) {
  46.     if (!this.ttl) {
  47.       return Promise.resolve(null);
  48.     }
  49.     return this.put(this.prefix + className, schema);
  50.   }
  51.  
  52.   getOneSchema(className) {
  53.     if (!this.ttl) {
  54.       return Promise.resolve(null);
  55.     }
  56.     return this.cache.get(this.prefix + className).then((schema) => {
  57.       if (schema) {
  58.         return Promise.resolve(schema);
  59.       }
  60.       return this.cache.get(this.prefix + MAIN_SCHEMA).then((cachedSchemas) => {
  61.         cachedSchemas = cachedSchemas || [];
  62.         schema = cachedSchemas.find((cachedSchema) => {
  63.           return cachedSchema.className === className;
  64.         });
  65.         if (schema) {
  66.           return Promise.resolve(schema);
  67.         }
  68.         return Promise.resolve(null);
  69.       });
  70.     });
  71.   }
  72.  
  73.   clear() {
  74.     // That clears all caches...
  75.     return this.cache.get(this.prefix + ALL_KEYS).then((allKeys) => {
  76.       if (!allKeys) {
  77.         return;
  78.       }
  79.       const promises = Object.keys(allKeys).map((key) => {
  80.         return this.cache.del(key);
  81.       });
  82.       return Promise.all(promises);
  83.     });
  84.   }
  85. }
  86.  
downloadSchemaCache.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