memory-cache.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * @copyright
  3. * Copyright © Microsoft Open Technologies, Inc.
  4. *
  5. * All Rights Reserved
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http: *www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  14. * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  15. * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
  16. * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
  17. *
  18. * See the Apache License, Version 2.0 for the specific language
  19. * governing permissions and limitations under the License.
  20. */
  21. 'use strict';
  22. var _ = require('underscore');
  23. /**
  24. * Constructs a new in memory token cache.
  25. * @constructor
  26. */
  27. function MemoryCache() {
  28. this._entries = [];
  29. }
  30. /**
  31. * Removes a collection of entries from the cache in a single batch operation.
  32. * @param {Array} entries An array of cache entries to remove.
  33. * @param {Function} callback This function is called when the operation is complete. Any error is provided as the
  34. * first parameter.
  35. */
  36. MemoryCache.prototype.remove = function(entries, callback) {
  37. var updatedEntries = _.filter(this._entries, function(element) {
  38. if (_.findWhere(entries, element)) {
  39. return false;
  40. }
  41. return true;
  42. });
  43. this._entries = updatedEntries;
  44. callback();
  45. };
  46. /**
  47. * Adds a collection of entries to the cache in a single batch operation.
  48. * @param {Array} entries An array of entries to add to the cache.
  49. * @param {Function} callback This function is called when the operation is complete. Any error is provided as the
  50. * first parameter.
  51. */
  52. MemoryCache.prototype.add = function(entries, callback) {
  53. // Remove any entries that are duplicates of the existing
  54. // cache elements.
  55. _.each(this._entries, function(element) {
  56. _.each(entries, function(addElement, index) {
  57. if (_.isEqual(element, addElement)) {
  58. entries[index] = null;
  59. }
  60. });
  61. });
  62. // Add the new entries to the end of the cache.
  63. entries = _.compact(entries);
  64. for (var i = 0; i < entries.length; i++) {
  65. this._entries.push(entries[i]);
  66. }
  67. callback(null, true);
  68. };
  69. /**
  70. * Finds all entries in the cache that match all of the passed in values.
  71. * @param {object} query This object will be compared to each entry in the cache. Any entries that
  72. * match all of the values in this object will be returned. All the values
  73. * in the passed in object must match values in a potentialy returned object
  74. * exactly. The returned object may have more values than the passed in query
  75. * object.
  76. * @param {TokenCacheFindCallback} callback
  77. */
  78. MemoryCache.prototype.find = function(query, callback) {
  79. var results = _.where(this._entries, query);
  80. callback(null, results);
  81. };
  82. module.exports = MemoryCache;