mirror of
https://github.com/frappe/books.git
synced 2025-01-26 08:38:27 +00:00
fix: frappe.db.getCachedValue
- CacheManager - Use db.getCachedValue in doc.getFrom
This commit is contained in:
parent
7198979fc7
commit
fef4f96e78
@ -1,5 +1,6 @@
|
|||||||
const frappe = require('frappejs');
|
const frappe = require('frappejs');
|
||||||
const Observable = require('frappejs/utils/observable');
|
const Observable = require('frappejs/utils/observable');
|
||||||
|
const CacheManager = require('frappejs/utils/cacheManager');
|
||||||
const Knex = require('knex');
|
const Knex = require('knex');
|
||||||
|
|
||||||
module.exports = class Database extends Observable {
|
module.exports = class Database extends Observable {
|
||||||
@ -7,6 +8,7 @@ module.exports = class Database extends Observable {
|
|||||||
super();
|
super();
|
||||||
this.initTypeMap();
|
this.initTypeMap();
|
||||||
this.connectionParams = {};
|
this.connectionParams = {};
|
||||||
|
this.cache = new CacheManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
@ -326,7 +328,16 @@ module.exports = class Database extends Observable {
|
|||||||
|
|
||||||
return this.knex(doctype)
|
return this.knex(doctype)
|
||||||
.where('name', doc.name)
|
.where('name', doc.name)
|
||||||
.update(formattedDoc);
|
.update(formattedDoc)
|
||||||
|
.then(() => {
|
||||||
|
let cacheKey = `${doctype}:${doc.name}`;
|
||||||
|
if (this.cache.hexists(cacheKey)) {
|
||||||
|
for (let fieldname in formattedDoc) {
|
||||||
|
let value = formattedDoc[fieldname];
|
||||||
|
this.cache.hset(cacheKey, fieldname, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
runDeleteOtherChildren(field, parent, added) {
|
runDeleteOtherChildren(field, parent, added) {
|
||||||
@ -364,7 +375,10 @@ module.exports = class Database extends Observable {
|
|||||||
let baseDoctype = meta.getBaseDocType();
|
let baseDoctype = meta.getBaseDocType();
|
||||||
await this.knex(baseDoctype)
|
await this.knex(baseDoctype)
|
||||||
.update({ name: newName })
|
.update({ name: newName })
|
||||||
.where('name', oldName);
|
.where('name', oldName)
|
||||||
|
.then(() => {
|
||||||
|
this.clearValueCache(doctype, oldName);
|
||||||
|
});
|
||||||
await frappe.db.commit();
|
await frappe.db.commit();
|
||||||
|
|
||||||
this.triggerChange(doctype, newName);
|
this.triggerChange(doctype, newName);
|
||||||
@ -447,7 +461,10 @@ module.exports = class Database extends Observable {
|
|||||||
async deleteOne(doctype, name) {
|
async deleteOne(doctype, name) {
|
||||||
return this.knex(doctype)
|
return this.knex(doctype)
|
||||||
.where('name', name)
|
.where('name', name)
|
||||||
.delete();
|
.delete()
|
||||||
|
.then(() => {
|
||||||
|
this.clearValueCache(doctype, name);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteChildren(parenttype, parent) {
|
deleteChildren(parenttype, parent) {
|
||||||
@ -493,6 +510,14 @@ module.exports = class Database extends Observable {
|
|||||||
return this.updateOne(doctype, doc);
|
return this.updateOne(doctype, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCachedValue(doctype, name, fieldname) {
|
||||||
|
let value = this.cache.hget(`${doctype}:${name}`, fieldname);
|
||||||
|
if (value == null) {
|
||||||
|
value = await this.getValue(doctype, name, fieldname);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
getAll({
|
getAll({
|
||||||
doctype,
|
doctype,
|
||||||
fields,
|
fields,
|
||||||
@ -609,6 +634,11 @@ module.exports = class Database extends Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearValueCache(doctype, name) {
|
||||||
|
let cacheKey = `${doctype}:${name}`;
|
||||||
|
this.cache.hclear(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
getColumnType(field) {
|
getColumnType(field) {
|
||||||
return this.typeMap[field.fieldtype];
|
return this.typeMap[field.fieldtype];
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,6 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
this.flags = {};
|
this.flags = {};
|
||||||
this.setup();
|
this.setup();
|
||||||
this.setValues(data);
|
this.setValues(data);
|
||||||
|
|
||||||
// clear fetch-values cache
|
|
||||||
frappe.db.on(
|
|
||||||
'change',
|
|
||||||
params => (this.fetchValuesCache[`${params.doctype}:${params.name}`] = {})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
@ -545,15 +539,9 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
.reduce((a, b) => a + b, 0);
|
.reduce((a, b) => a + b, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getFrom(doctype, name, fieldname) {
|
getFrom(doctype, name, fieldname) {
|
||||||
if (!name) return '';
|
if (!name) return '';
|
||||||
let _values =
|
return frappe.db.getCachedValue(doctype, name, fieldname);
|
||||||
this.fetchValuesCache[`${doctype}:${name}`] ||
|
|
||||||
(this.fetchValuesCache[`${doctype}:${name}`] = {});
|
|
||||||
if (!_values[fieldname]) {
|
|
||||||
_values[fieldname] = await frappe.db.getValue(doctype, name, fieldname);
|
|
||||||
}
|
|
||||||
return _values[fieldname];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isNew() {
|
isNew() {
|
||||||
|
41
utils/cacheManager.js
Normal file
41
utils/cacheManager.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
class CacheManager {
|
||||||
|
constructor() {
|
||||||
|
this.keyValueCache = {};
|
||||||
|
this.hashCache = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
getValue(key) {
|
||||||
|
return this.keyValueCache[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
setValue(key, value) {
|
||||||
|
this.keyValueCache[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
clearValue(key) {
|
||||||
|
this.keyValueCache[key] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
hget(hashName, key) {
|
||||||
|
return (this.hashCache[hashName] || {})[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
hset(hashName, key, value) {
|
||||||
|
this.hashCache[hashName] = this.hashCache[hashName] || {};
|
||||||
|
this.hashCache[hashName][key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
hclear(hashName, key) {
|
||||||
|
if (key) {
|
||||||
|
(this.hashCache[hashName] || {})[key] = null;
|
||||||
|
} else {
|
||||||
|
this.hashCache[hashName] = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hexists(hashName) {
|
||||||
|
return this.hashCache[hashName] != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = CacheManager;
|
Loading…
x
Reference in New Issue
Block a user