Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. class QuickLRU {
  3. constructor(options = {}) {
  4. if (!(options.maxSize && options.maxSize > 0)) {
  5. throw new TypeError('`maxSize` must be a number greater than 0');
  6. }
  7. this.maxSize = options.maxSize;
  8. this.cache = new Map();
  9. this.oldCache = new Map();
  10. this._size = 0;
  11. }
  12. _set(key, value) {
  13. this.cache.set(key, value);
  14. this._size++;
  15. if (this._size >= this.maxSize) {
  16. this._size = 0;
  17. this.oldCache = this.cache;
  18. this.cache = new Map();
  19. }
  20. }
  21. get(key) {
  22. if (this.cache.has(key)) {
  23. return this.cache.get(key);
  24. }
  25. if (this.oldCache.has(key)) {
  26. const value = this.oldCache.get(key);
  27. this.oldCache.delete(key);
  28. this._set(key, value);
  29. return value;
  30. }
  31. }
  32. set(key, value) {
  33. if (this.cache.has(key)) {
  34. this.cache.set(key, value);
  35. } else {
  36. this._set(key, value);
  37. }
  38. return this;
  39. }
  40. has(key) {
  41. return this.cache.has(key) || this.oldCache.has(key);
  42. }
  43. peek(key) {
  44. if (this.cache.has(key)) {
  45. return this.cache.get(key);
  46. }
  47. if (this.oldCache.has(key)) {
  48. return this.oldCache.get(key);
  49. }
  50. }
  51. delete(key) {
  52. const deleted = this.cache.delete(key);
  53. if (deleted) {
  54. this._size--;
  55. }
  56. return this.oldCache.delete(key) || deleted;
  57. }
  58. clear() {
  59. this.cache.clear();
  60. this.oldCache.clear();
  61. this._size = 0;
  62. }
  63. * keys() {
  64. for (const [key] of this) {
  65. yield key;
  66. }
  67. }
  68. * values() {
  69. for (const [, value] of this) {
  70. yield value;
  71. }
  72. }
  73. * [Symbol.iterator]() {
  74. for (const item of this.cache) {
  75. yield item;
  76. }
  77. for (const item of this.oldCache) {
  78. const [key] = item;
  79. if (!this.cache.has(key)) {
  80. yield item;
  81. }
  82. }
  83. }
  84. get size() {
  85. let oldCacheSize = 0;
  86. for (const key of this.oldCache.keys()) {
  87. if (!this.cache.has(key)) {
  88. oldCacheSize++;
  89. }
  90. }
  91. return this._size + oldCacheSize;
  92. }
  93. }
  94. module.exports = QuickLRU;