memory.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var crypto = require('crypto')
  10. , Store = require('../store');
  11. /**
  12. * Exports the constructor.
  13. */
  14. exports = module.exports = Memory;
  15. Memory.Client = Client;
  16. /**
  17. * Memory store
  18. *
  19. * @api public
  20. */
  21. function Memory (opts) {
  22. Store.call(this, opts);
  23. };
  24. /**
  25. * Inherits from Store.
  26. */
  27. Memory.prototype.__proto__ = Store.prototype;
  28. /**
  29. * Publishes a message.
  30. *
  31. * @api private
  32. */
  33. Memory.prototype.publish = function () { };
  34. /**
  35. * Subscribes to a channel
  36. *
  37. * @api private
  38. */
  39. Memory.prototype.subscribe = function () { };
  40. /**
  41. * Unsubscribes
  42. *
  43. * @api private
  44. */
  45. Memory.prototype.unsubscribe = function () { };
  46. /**
  47. * Client constructor
  48. *
  49. * @api private
  50. */
  51. function Client () {
  52. Store.Client.apply(this, arguments);
  53. this.data = {};
  54. };
  55. /**
  56. * Inherits from Store.Client
  57. */
  58. Client.prototype.__proto__ = Store.Client;
  59. /**
  60. * Gets a key
  61. *
  62. * @api public
  63. */
  64. Client.prototype.get = function (key, fn) {
  65. fn(null, this.data[key] === undefined ? null : this.data[key]);
  66. return this;
  67. };
  68. /**
  69. * Sets a key
  70. *
  71. * @api public
  72. */
  73. Client.prototype.set = function (key, value, fn) {
  74. this.data[key] = value;
  75. fn && fn(null);
  76. return this;
  77. };
  78. /**
  79. * Has a key
  80. *
  81. * @api public
  82. */
  83. Client.prototype.has = function (key, fn) {
  84. fn(null, key in this.data);
  85. };
  86. /**
  87. * Deletes a key
  88. *
  89. * @api public
  90. */
  91. Client.prototype.del = function (key, fn) {
  92. delete this.data[key];
  93. fn && fn(null);
  94. return this;
  95. };
  96. /**
  97. * Destroys the client.
  98. *
  99. * @param {Number} number of seconds to expire data
  100. * @api private
  101. */
  102. Client.prototype.destroy = function (expiration) {
  103. if ('number' != typeof expiration) {
  104. this.data = {};
  105. } else {
  106. var self = this;
  107. setTimeout(function () {
  108. self.data = {};
  109. }, expiration * 1000);
  110. }
  111. return this;
  112. };