redis-backend.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var Logger = require('memdb-logger');
  18. var redis = P.promisifyAll(require('redis'));
  19. var RedisBackend = function(opts){
  20. opts = opts || {};
  21. this.config = {
  22. host : opts.host || '127.0.0.1',
  23. port : opts.port || 6379,
  24. db : opts.db || 0,
  25. options : opts.option || {},
  26. prefix : opts.prefix || '',
  27. };
  28. this.conn = null;
  29. this.logger = Logger.getLogger('memdb', __filename, 'shard:' + opts.shardId);
  30. };
  31. var proto = RedisBackend.prototype;
  32. proto.start = function(){
  33. this.conn = redis.createClient(this.config.port, this.config.host, {retry_max_delay : 10 * 1000});
  34. var self = this;
  35. this.conn.on('error', function(err){
  36. self.logger.error(err.stack);
  37. });
  38. this.conn.select(this.config.db);
  39. this.logger.debug('backend redis connected to %s:%s:%s', this.config.host, this.config.port, this.config.db);
  40. };
  41. proto.stop = function(){
  42. this.logger.debug('backend redis stop');
  43. return this.conn.quitAsync();
  44. };
  45. proto.get = function(name, id){
  46. this.logger.debug('backend redis get(%s, %s)', name, id);
  47. return P.bind(this)
  48. .then(function(){
  49. return this.conn.hmgetAsync(this.config.prefix + name, id);
  50. })
  51. .then(function(ret){
  52. ret = ret[0];
  53. return JSON.parse(ret);
  54. });
  55. };
  56. // Return an async iterator with .next(cb) signature
  57. proto.getAll = function(name){
  58. throw new Error('not implemented');
  59. };
  60. // delete when doc is null
  61. proto.set = function(name, id, doc){
  62. this.logger.debug('backend redis set(%s, %s)', name, id);
  63. if(!!doc){
  64. return this.conn.hmsetAsync(this.config.prefix + name, id, JSON.stringify(doc));
  65. }
  66. else{
  67. return this.conn.hdelAsync(this.config.prefix + name, id);
  68. }
  69. };
  70. // items : [{name, id, doc}]
  71. proto.setMulti = function(items){
  72. this.logger.debug('backend redis setMulti');
  73. var multi = this.conn.multi();
  74. var self = this;
  75. items.forEach(function(item){
  76. if(!!item.doc){
  77. multi = multi.hmset(self.config.prefix + item.name, item.id, JSON.stringify(item.doc));
  78. }
  79. else{
  80. multi = multi.hdel(self.config.prefix + item.name, item.id);
  81. }
  82. });
  83. return multi.execAsync();
  84. };
  85. // drop table or database
  86. proto.drop = function(name){
  87. this.logger.debug('backend redis drop %s', name);
  88. if(!!name){
  89. throw new Error('not implemented');
  90. //this.conn.delAsync(this.config.prefix + name);
  91. }
  92. else{
  93. this.conn.flushdbAsync();
  94. }
  95. };
  96. proto.getCollectionNames = function(){
  97. throw new Error('not implemented');
  98. };
  99. module.exports = RedisBackend;