ConfigBase.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. cc.Class({
  2. name: 'ConfigBase',
  3. properties: {
  4. mainKey: "",
  5. indexsCache:null,
  6. multiple:[],
  7. multipleCache:null,
  8. table:null,
  9. tableName:"",
  10. isLoadFinish:false,
  11. },
  12. ctor() {
  13. },
  14. initTable(tableName, mainKey, multipleKey) {
  15. this.tableName = tableName;
  16. this.mainKey = mainKey;
  17. this.multiple = multipleKey;
  18. this.indexsCache = {};
  19. this.multipleCache = {};
  20. return this
  21. },
  22. setTable(table) {
  23. this.table = table
  24. },
  25. // 从主键中拿某一行数据
  26. getByMainKey(idx) {
  27. // 从缓存中拿数据
  28. let data = this.indexsCache[idx]
  29. if (data != undefined) {
  30. return data;
  31. }
  32. // 找数据
  33. for (let d of this.table) {
  34. if (d[this.mainKey] == idx) {
  35. data = d;
  36. break;
  37. }
  38. }
  39. // 缓存数据
  40. if (data) {
  41. this.indexsCache[data[this.mainKey]] = data;
  42. }
  43. return data;
  44. },
  45. // 从多主键中,拿数据
  46. getByMultipleKey() {
  47. // 参数跟多主键数量对应不上
  48. if (arguments.length != this.multiple.length) {
  49. if (arguments.length <= 0) {
  50. cc.log("multiple key is null");
  51. } else {
  52. cc.log("multiple key num not match curNum :" + arguments.length + " need num " + this.multiple.length);
  53. }
  54. return null;
  55. }
  56. // 从缓存中拿数据
  57. let curCache = this.findMultipleCache(arguments);
  58. if (curCache != null)
  59. {
  60. return curCache;
  61. }
  62. // 查找数据
  63. return this.getAndAddMultiple(arguments);
  64. },
  65. // 拿多主键数据并缓存
  66. getAndAddMultiple(args)
  67. {
  68. var result = [];
  69. // 结果拿出来
  70. for (let d of this.table)
  71. {
  72. let isMatch = true;
  73. for (let i = 0; i < this.multiple.length; i++) {
  74. const k = this.multiple[i];
  75. if (d[k] != args[i]) {
  76. isMatch = false;
  77. break
  78. }
  79. }
  80. if (isMatch)
  81. {
  82. result.push(d);
  83. }
  84. }
  85. // 添加到缓存区
  86. let curCache = this.multipleCache;
  87. for (let i = 0; i < args.length - 1; i++) {
  88. let k = args[i];
  89. let tmpCache = curCache[k];
  90. if (tmpCache == undefined)
  91. {
  92. curCache[k] = tmpCache = {}
  93. }
  94. curCache = tmpCache;
  95. }
  96. curCache[args[args.length - 1]] = result;
  97. return result;
  98. },
  99. // 从多主键缓存中拿数据
  100. findMultipleCache(args)
  101. {
  102. // 索引缓存
  103. let curCache = this.multipleCache;
  104. let isFindCache = true;
  105. for(let p of args) {
  106. if (curCache[p] == undefined) {
  107. isFindCache = false
  108. break
  109. }
  110. curCache = curCache[p]
  111. }
  112. if (isFindCache)
  113. {
  114. return curCache;
  115. }
  116. return null;
  117. },
  118. })