kuzzleio / kuzzle

Open-source Back-end, self-hostable & ready to use - Real-time, storage, advanced search - Web, Apps, Mobile, IoT -
https://kuzzle.io
Apache License 2.0
1.44k stars 124 forks source link

Enable cache Engine for plugin's repositories #875

Closed ballinette closed 4 years ago

ballinette commented 7 years ago

For now, the PluginRepository exposes methods to manage contents only in the storage engine, not in the cache engine (see https://github.com/kuzzleio/kuzzle/blob/rc.x/lib/api/core/models/repositories/pluginRepository.js and https://github.com/kuzzleio/kuzzle/blob/rc.x/lib/api/core/plugins/pluginContext.js#L112)

It would be fine to also let plugins to manage content directly in Redis, using the cacheEngine.

either activate the cacheEngine by default and let its usage transparent for the plugin developer (as main Repository class does) or expose to the plugins some methods that use the storageEngine, and some others that use the cacheEngine, like that (in pluginContext.js):

this.constructors.Repository = function PluginContextRepository (collection, ObjectConstructor = null) {
  if (!collection) {
    throw new PluginImplementationError('The collection must be specified.');
  }

  const pluginRepository = new PluginRepository(kuzzle, internalEngineIndex, collection);
  pluginRepository.init({databaseEngine: pluginInternalEngine, ObjectConstructor});

  return {
    // Actual methods that use the storage engine :
    search: pluginRepository.search.bind(pluginRepository),
    get: pluginRepository.loadOneFromDatabase.bind(pluginRepository),
    mGet: pluginRepository.loadMultiFromDatabase.bind(pluginRepository),
    delete: pluginRepository.deleteFromDatabase.bind(pluginRepository),
    create: (object, options = {}) => pluginRepository.pestistToDatabase(object, Object.assign({method: 'create'}, options)),
    createOrReplace: (object, options = {}) => pluginRepository.pestistToDatabase(object, Object.assign({method: 'createOrReplace'}, options)),
    replace: (object, options = {}) => pluginRepository.pestistToDatabase(object, Object.assign({method: 'replace'}, options)),
    update: (object, options = {}) => pluginRepository.pestistToDatabase(object, Object.assign({method: 'update'}, options)),

    // New methods that use the cache engine :
    cache: {
      read: pluginRepository.loadFromCache.bind(pluginRepository),
      write: pluginRepository.persistToCache.bind(pluginRepository)
      remove: pluginRepository.deleteFromCache.bind(pluginRepository),
    }
  };
}
Aschen commented 4 years ago

Too old.