spice-js 2.6.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ function _connect() {
12
12
  _connect = _asyncToGenerator(function* () {
13
13
  try {
14
14
  spice.cache_providers = {};
15
+ spice.monitor = {};
15
16
 
16
17
  for (var key of Object.keys(spice.config.cache.providers)) {
17
18
  spice.cache_providers[key] = new spice.config.cache.providers[key](spice.config.cache.drivers[key] || {});
@@ -455,6 +455,25 @@ class SpiceModel {
455
455
  })();
456
456
  }
457
457
 
458
+ shouldForceRefresh(response) {
459
+ var monitor_record = _.get(spice.monitor, this.type);
460
+
461
+ if (monitor_record == undefined) {
462
+ return false;
463
+ }
464
+
465
+ if (monitor_record > (response == null ? void 0 : response.time)) {
466
+ return true;
467
+ }
468
+
469
+ return false;
470
+ }
471
+
472
+ setMonitor() {
473
+ var current_time = new Date().getTime();
474
+ spice.monitor[this.type] = current_time; //console.log("Monitor Set", spice.monitor);
475
+ }
476
+
458
477
  get(args) {
459
478
  var _this3 = this;
460
479
 
@@ -471,20 +490,14 @@ class SpiceModel {
471
490
 
472
491
  if (_this3.shouldCache(_this3.type)) {
473
492
  var cached_results = yield _this3.getCacheProviderObject(_this3.type).get(key);
493
+ results = cached_results == null ? void 0 : cached_results.value;
474
494
 
475
- if (cached_results) {
476
- //calculate time taken to get data from Cache
477
- results = cached_results;
478
- /* if (this.type == "user") {
479
- console.log("results from Cache", results);
480
- } */
481
- } else {
495
+ if ((cached_results == null ? void 0 : cached_results.value) == undefined || _this3.shouldForceRefresh(cached_results)) {
482
496
  results = yield _this3.database.get(args.id);
483
- /* if (this.type == "user") {
484
- console.log("results from DB", results);
485
- } */
486
-
487
- yield _this3.getCacheProviderObject(_this3.type).set(key, results, _this3.getCacheConfig(_this3.type));
497
+ yield _this3.getCacheProviderObject(_this3.type).set(key, {
498
+ value: results,
499
+ time: new Date().getTime()
500
+ }, _this3.getCacheConfig(_this3.type));
488
501
  }
489
502
  } else {
490
503
  results = yield _this3.database.get(args.id);
@@ -545,13 +558,15 @@ class SpiceModel {
545
558
  if (args.ids.length > 0) {
546
559
  if (_this5.shouldCache(_this5.type)) {
547
560
  var cached_results = yield _this5.getCacheProviderObject(_this5.type).get(key);
561
+ results = cached_results == null ? void 0 : cached_results.value;
548
562
 
549
- if (cached_results) {
550
- results = cached_results;
551
- } else {
563
+ if ((cached_results == null ? void 0 : cached_results.value) == undefined || _this5.shouldForceRefresh(cached_results)) {
552
564
  results = yield _this5.database.multi_get(args.ids, true);
553
565
 
554
- _this5.getCacheProviderObject(_this5.type).set(key, results, _this5.getCacheConfig(_this5.type));
566
+ _this5.getCacheProviderObject(_this5.type).set(key, {
567
+ value: results,
568
+ time: new Date().getTime()
569
+ }, _this5.getCacheConfig(_this5.type));
555
570
  }
556
571
  } else {
557
572
  results = yield _this5.database.multi_get(args.ids, true);
@@ -635,6 +650,8 @@ class SpiceModel {
635
650
  var db_data = form || _this7;
636
651
  yield _this7.database.update(args.id, db_data);
637
652
 
653
+ _this7.setMonitor();
654
+
638
655
  if (args.skip_hooks != true) {
639
656
  yield _this7.run_hook(_extends({}, _this7, {
640
657
  id: args.id
@@ -682,6 +699,9 @@ class SpiceModel {
682
699
  yield _this8.run_hook(workingForm, "create", "before");
683
700
  workingForm = yield _this8.do_serialize(workingForm, "write", {}, args);
684
701
  var results = yield _this8.database.insert(id, workingForm, args.expiry);
702
+
703
+ _this8.setMonitor();
704
+
685
705
  yield _this8.run_hook(_extends({}, results, {
686
706
  id
687
707
  }), "create", "after");
@@ -714,6 +734,8 @@ class SpiceModel {
714
734
 
715
735
  if (args.hard) {
716
736
  delete_response = yield _this9.database.delete(args.id);
737
+
738
+ _this9.setMonitor();
717
739
  } else {
718
740
  delete results["id"];
719
741
  results.deleted = true;
@@ -783,13 +805,15 @@ class SpiceModel {
783
805
  if (args.ids.length > 0) {
784
806
  if (_this10.shouldCache(_this10.type)) {
785
807
  var cached_results = yield _this10.getCacheProviderObject(_this10.type).get(key);
808
+ results = cached_results == null ? void 0 : cached_results.value;
786
809
 
787
- if (cached_results) {
788
- results = cached_results;
789
- } else {
810
+ if ((cached_results == null ? void 0 : cached_results.value) == undefined || _this10.shouldForceRefresh(cached_results)) {
790
811
  results = yield _this10.database.query(query);
791
812
 
792
- _this10.getCacheProviderObject(_this10.type).set(key, results, _this10.getCacheConfig(_this10.type));
813
+ _this10.getCacheProviderObject(_this10.type).set(key, {
814
+ value: results,
815
+ time: new Date().getTime()
816
+ }, _this10.getCacheConfig(_this10.type));
793
817
  }
794
818
  } else {
795
819
  results = yield _this10.database.query(query);
@@ -800,12 +824,15 @@ class SpiceModel {
800
824
  if (_this10.shouldCache(_this10.type)) {
801
825
  var _cached_results = yield _this10.getCacheProviderObject(_this10.type).get(key);
802
826
 
803
- if (_cached_results) {
804
- results = _cached_results;
805
- } else {
827
+ results = _cached_results == null ? void 0 : _cached_results.value;
828
+
829
+ if ((_cached_results == null ? void 0 : _cached_results.value) == undefined || _this10.shouldForceRefresh(_cached_results)) {
806
830
  results = yield _this10.database.full_text_search(_this10.type, query || "", args.limit, args.offset);
807
831
 
808
- _this10.getCacheProviderObject(_this10.type).set(key, results, _this10.getCacheConfig(_this10.type));
832
+ _this10.getCacheProviderObject(_this10.type).set(key, {
833
+ value: results,
834
+ time: new Date().getTime()
835
+ }, _this10.getCacheConfig(_this10.type));
809
836
  }
810
837
  } else {
811
838
  results = yield _this10.database.full_text_search(_this10.type, query || "", args.limit, args.offset);
@@ -814,12 +841,15 @@ class SpiceModel {
814
841
  if (_this10.shouldCache(_this10.type)) {
815
842
  var _cached_results2 = yield _this10.getCacheProviderObject(_this10.type).get(key);
816
843
 
817
- if (_cached_results2) {
818
- results = _cached_results2;
819
- } else {
844
+ results = _cached_results2 == null ? void 0 : _cached_results2.value;
845
+
846
+ if ((_cached_results2 == null ? void 0 : _cached_results2.value) == undefined || _this10.shouldForceRefresh(_cached_results2)) {
820
847
  results = yield _this10.database.search(_this10.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
821
848
 
822
- _this10.getCacheProviderObject(_this10.type).set(key, results, _this10.getCacheConfig(_this10.type));
849
+ _this10.getCacheProviderObject(_this10.type).set(key, {
850
+ value: results,
851
+ time: new Date().getTime()
852
+ }, _this10.getCacheConfig(_this10.type));
823
853
  }
824
854
  } else {
825
855
  results = yield _this10.database.search(_this10.type, args.columns || "", query || "", args.limit, args.offset, args.sort, args.do_count, args.statement_consistent);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-js",
3
- "version": "2.6.0",
3
+ "version": "2.6.1",
4
4
  "description": "spice",
5
5
  "main": "build/index.js",
6
6
  "repository": {
@@ -3,6 +3,7 @@
3
3
  async function connect() {
4
4
  try {
5
5
  spice.cache_providers = {};
6
+ spice.monitor = {};
6
7
  for (let key of Object.keys(spice.config.cache.providers)) {
7
8
  spice.cache_providers[key] = new spice.config.cache.providers[key](
8
9
  spice.config.cache.drivers[key] || {}
@@ -386,6 +386,23 @@ export default class SpiceModel {
386
386
  return false;
387
387
  }
388
388
 
389
+ shouldForceRefresh(response) {
390
+ let monitor_record = _.get(spice.monitor, this.type);
391
+ if (monitor_record == undefined) {
392
+ return false;
393
+ }
394
+ if (monitor_record > response?.time) {
395
+ return true;
396
+ }
397
+ return false;
398
+ }
399
+
400
+ setMonitor() {
401
+ let current_time = new Date().getTime();
402
+ spice.monitor[this.type] = current_time;
403
+ //console.log("Monitor Set", spice.monitor);
404
+ }
405
+
389
406
  async get(args) {
390
407
  try {
391
408
  if (!args) {
@@ -399,21 +416,15 @@ export default class SpiceModel {
399
416
  let cached_results = await this.getCacheProviderObject(this.type).get(
400
417
  key
401
418
  );
402
- if (cached_results) {
403
- //calculate time taken to get data from Cache
404
- results = cached_results;
405
-
406
- /* if (this.type == "user") {
407
- console.log("results from Cache", results);
408
- } */
409
- } else {
419
+ results = cached_results?.value;
420
+ if (
421
+ cached_results?.value == undefined ||
422
+ this.shouldForceRefresh(cached_results)
423
+ ) {
410
424
  results = await this.database.get(args.id);
411
- /* if (this.type == "user") {
412
- console.log("results from DB", results);
413
- } */
414
425
  await this.getCacheProviderObject(this.type).set(
415
426
  key,
416
- results,
427
+ { value: results, time: new Date().getTime() },
417
428
  this.getCacheConfig(this.type)
418
429
  );
419
430
  }
@@ -474,13 +485,15 @@ export default class SpiceModel {
474
485
  let cached_results = await this.getCacheProviderObject(this.type).get(
475
486
  key
476
487
  );
477
- if (cached_results) {
478
- results = cached_results;
479
- } else {
488
+ results = cached_results?.value;
489
+ if (
490
+ cached_results?.value == undefined ||
491
+ this.shouldForceRefresh(cached_results)
492
+ ) {
480
493
  results = await this.database.multi_get(args.ids, true);
481
494
  this.getCacheProviderObject(this.type).set(
482
495
  key,
483
- results,
496
+ { value: results, time: new Date().getTime() },
484
497
  this.getCacheConfig(this.type)
485
498
  );
486
499
  }
@@ -561,6 +574,8 @@ export default class SpiceModel {
561
574
  let db_data = form || this;
562
575
 
563
576
  await this.database.update(args.id, db_data);
577
+ this.setMonitor();
578
+
564
579
  if (args.skip_hooks != true) {
565
580
  await this.run_hook(
566
581
  {
@@ -610,6 +625,7 @@ export default class SpiceModel {
610
625
  workingForm = await this.do_serialize(workingForm, "write", {}, args);
611
626
 
612
627
  let results = await this.database.insert(id, workingForm, args.expiry);
628
+ this.setMonitor();
613
629
  await this.run_hook(
614
630
  {
615
631
  ...results,
@@ -644,6 +660,7 @@ export default class SpiceModel {
644
660
  let delete_response = {};
645
661
  if (args.hard) {
646
662
  delete_response = await this.database.delete(args.id);
663
+ this.setMonitor();
647
664
  } else {
648
665
  delete results["id"];
649
666
  results.deleted = true;
@@ -713,13 +730,15 @@ export default class SpiceModel {
713
730
  let cached_results = await this.getCacheProviderObject(
714
731
  this.type
715
732
  ).get(key);
716
- if (cached_results) {
717
- results = cached_results;
718
- } else {
733
+ results = cached_results?.value;
734
+ if (
735
+ cached_results?.value == undefined ||
736
+ this.shouldForceRefresh(cached_results)
737
+ ) {
719
738
  results = await this.database.query(query);
720
739
  this.getCacheProviderObject(this.type).set(
721
740
  key,
722
- results,
741
+ { value: results, time: new Date().getTime() },
723
742
  this.getCacheConfig(this.type)
724
743
  );
725
744
  }
@@ -733,9 +752,11 @@ export default class SpiceModel {
733
752
  let cached_results = await this.getCacheProviderObject(
734
753
  this.type
735
754
  ).get(key);
736
- if (cached_results) {
737
- results = cached_results;
738
- } else {
755
+ results = cached_results?.value;
756
+ if (
757
+ cached_results?.value == undefined ||
758
+ this.shouldForceRefresh(cached_results)
759
+ ) {
739
760
  results = await this.database.full_text_search(
740
761
  this.type,
741
762
  query || "",
@@ -744,7 +765,7 @@ export default class SpiceModel {
744
765
  );
745
766
  this.getCacheProviderObject(this.type).set(
746
767
  key,
747
- results,
768
+ { value: results, time: new Date().getTime() },
748
769
  this.getCacheConfig(this.type)
749
770
  );
750
771
  }
@@ -761,9 +782,11 @@ export default class SpiceModel {
761
782
  let cached_results = await this.getCacheProviderObject(
762
783
  this.type
763
784
  ).get(key);
764
- if (cached_results) {
765
- results = cached_results;
766
- } else {
785
+ results = cached_results?.value;
786
+ if (
787
+ cached_results?.value == undefined ||
788
+ this.shouldForceRefresh(cached_results)
789
+ ) {
767
790
  results = await this.database.search(
768
791
  this.type,
769
792
  args.columns || "",
@@ -776,7 +799,7 @@ export default class SpiceModel {
776
799
  );
777
800
  this.getCacheProviderObject(this.type).set(
778
801
  key,
779
- results,
802
+ { value: results, time: new Date().getTime() },
780
803
  this.getCacheConfig(this.type)
781
804
  );
782
805
  }