Skip to content

Commit 63241cc

Browse files
committed
Rename & disable identity cache by default.
1 parent c912271 commit 63241cc

28 files changed

+97
-84
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- '0.10'
54
- '0.12'
5+
- '4'
66
before_script:
77
- mysql -e 'create database orm_test;'
88
- psql -c 'create database orm_test;' -U postgres

Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### v3.0.0
2+
- Rename cache -> identityCache & disabled by default (#350, #564, #626, #672, #684, #694, #721)
3+
14
### v2.1.29
25
- Fix hasOne association when ID is 0 (#681)
36
- Fix global var leak (#682)

Readme.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ orm.connect("mysql://username:password@host/database", function (err, db) {
7070
});
7171

7272
// add the table to the database
73-
db.sync(function(err) {
73+
db.sync(function(err) {
7474
if (err) throw err;
7575

7676
// add a row to the person table
@@ -90,7 +90,7 @@ orm.connect("mysql://username:password@host/database", function (err, db) {
9090
// err.msg = "under-age";
9191
});
9292
});
93-
93+
9494
});
9595
});
9696
});
@@ -278,9 +278,9 @@ var Person = db.define("person", {
278278

279279
Other options:
280280

281-
- `cache` : (default: `true`) Set it to `false` to disable Instance cache ([Singletons](#singleton)) or set a timeout value (in seconds);
282-
- `autoSave` : (default: `false`) Set it to `true` to save an Instance right after changing any property;
283-
- `autoFetch` : (default: `false`) Set it to `true` to fetch associations when fetching an instance from the database;
281+
- `identityCache` : (default: `false`) Set it to `true` to enable identity cache ([Singletons](#singleton)) or set a timeout value (in seconds);
282+
- `autoSave` : (default: `false`) Set it to `true` to save an Instance right after changing any property;
283+
- `autoFetch` : (default: `false`) Set it to `true` to fetch associations when fetching an instance from the database;
284284
- `autoFetchLimit` : (default: `1`) If `autoFetch` is enabled this defines how many hoops (associations of associations)
285285
you want it to automatically fetch.
286286

@@ -514,30 +514,32 @@ db.driver.execQuery(
514514
)
515515
```
516516

517-
### Caching & Integrity
517+
### Identity pattern
518+
519+
You can use the identity pattern (turned off by default). If enabled, multiple different queries will result in the same result - you will
520+
get the same object. If you have other systems that can change your database or you need to call some manual SQL queries,
521+
you shouldn't use this feature. It is also know to cause some problems with complex
522+
autofetch relationships. Use at your own risk.
518523

519-
Model instances are cached. If multiple different queries will result in the same result, you will
520-
get the same object. If you have other systems that can change your database (or you're developing and need
521-
to make some manual changes) you should remove this feature by disabling cache. This can be done when you're
522-
defining the Model.
524+
It can be enabled/disabled per model:
523525

524526
```js
525527
var Person = db.define('person', {
526-
name : String
528+
name : String
527529
}, {
528-
cache : false
530+
identityCache : true
529531
});
530532
```
531533

532534
and also globally:
533535

534536
```js
535537
orm.connect('...', function(err, db) {
536-
db.settings.set('instance.cache', false);
538+
db.settings.set('instance.identityCache', true);
537539
});
538540
```
539541

540-
The cache can be configured to expire after a period of time by passing in a number instead of a
542+
The identity cache can be configured to expire after a period of time by passing in a number instead of a
541543
boolean. The number will be considered the cache timeout in seconds (you can use floating point).
542544

543545
**Note**: One exception about Caching is that it won't be used if an instance is not saved. For example, if

lib/Associations/Extend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.prepare = function (db, Model, associations) {
3030
}
3131

3232
var modelOpts = _.extend(
33-
_.pick(opts, 'cache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'),
33+
_.pick(opts, 'identityCache', 'autoSave', 'cascadeRemove', 'hooks', 'methods', 'validations'),
3434
{
3535
id : Object.keys(association.field),
3636
extension : true,

lib/LazyLoad.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
1414
var conditions = {};
1515
conditions[Model.id] = Instance[Model.id];
1616

17-
Model.find(conditions, { cache: false }).only(Model.id.concat(property)).first(function (err, item) {
17+
Model.find(conditions, { identityCache: false }).only(Model.id.concat(property)).first(function (err, item) {
1818
return cb(err, item ? item[property] : null);
1919
});
2020

@@ -27,7 +27,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
2727
var conditions = {};
2828
conditions[Model.id] = Instance[Model.id];
2929

30-
Model.find(conditions, { cache: false }).only(Model.id.concat(property)).first(function (err, item) {
30+
Model.find(conditions, { identityCache: false }).only(Model.id.concat(property)).first(function (err, item) {
3131
if (err) {
3232
return cb(err);
3333
}
@@ -49,7 +49,7 @@ function addLazyLoadProperty(name, Instance, Model, property) {
4949
var conditions = {};
5050
conditions[Model.id] = Instance[Model.id];
5151

52-
Model.find(conditions, { cache: false }).first(function (err, item) {
52+
Model.find(conditions, { identityCache: false }).first(function (err, item) {
5353
if (err) {
5454
return cb(err);
5555
}

lib/Model.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ function Model(opts) {
298298
var uid = opts.driver.uid + "/" + opts.table + "/" + ids.join("/");
299299

300300
Singleton.get(uid, {
301-
cache : (options.hasOwnProperty("cache") ? options.cache : opts.cache),
302-
save_check : opts.settings.get("instance.cacheSaveCheck")
301+
identityCache : (options.hasOwnProperty("identityCache") ? options.identityCache : opts.identityCache),
302+
saveCheck : opts.settings.get("instance.identityCacheSaveCheck")
303303
}, function (cb) {
304304
return createInstance(data[0], {
305305
uid : uid,
@@ -365,8 +365,8 @@ function Model(opts) {
365365
}
366366
}
367367

368-
if (!options.hasOwnProperty("cache")) {
369-
options.cache = opts.cache;
368+
if (!options.hasOwnProperty("identityCache")) {
369+
options.identityCache = opts.identityCache;
370370
}
371371
if (!options.hasOwnProperty("autoFetchLimit")) {
372372
options.autoFetchLimit = opts.autoFetchLimit;
@@ -396,20 +396,20 @@ function Model(opts) {
396396
properties : allProperties,
397397
keyProperties: keyProperties,
398398
newInstance : function (data, cb) {
399-
// We need to do the rename before we construct the UID & do the cache lookup
400-
// because the cache is loaded using propertyName rather than fieldName
401-
Utilities.renameDatastoreFieldsToPropertyNames(data, fieldToPropertyMap);
399+
// We need to do the rename before we construct the UID & do the cache lookup
400+
// because the cache is loaded using propertyName rather than fieldName
401+
Utilities.renameDatastoreFieldsToPropertyNames(data, fieldToPropertyMap);
402402

403-
// Construct UID
403+
// Construct UID
404404
var uid = opts.driver.uid + "/" + opts.table + (merge ? "+" + merge.from.table : "");
405405
for (var i = 0; i < opts.keys.length; i++) {
406406
uid += "/" + data[opts.keys[i]];
407407
}
408408

409-
// Now we can do the cache lookup
409+
// Now we can do the cache lookup
410410
Singleton.get(uid, {
411-
cache : options.cache,
412-
save_check : opts.settings.get("instance.cacheSaveCheck")
411+
identityCache : options.identityCache,
412+
saveCheck : opts.settings.get("instance.identityCacheSaveCheck")
413413
}, function (cb) {
414414
return createInstance(data, {
415415
uid : uid,

lib/ORM.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ ORM.prototype.define = function (name, properties, opts) {
231231
properties : properties,
232232
extension : opts.extension || false,
233233
indexes : opts.indexes || [],
234-
cache : opts.hasOwnProperty("cache") ? opts.cache : this.settings.get("instance.cache"),
234+
identityCache : opts.hasOwnProperty("identityCache") ? opts.identityCache : this.settings.get("instance.identityCache"),
235235
keys : opts.id,
236236
autoSave : opts.hasOwnProperty("autoSave") ? opts.autoSave : this.settings.get("instance.autoSave"),
237237
autoFetch : opts.hasOwnProperty("autoFetch") ? opts.autoFetch : this.settings.get("instance.autoFetch"),

lib/Settings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ var default_settings = {
66
required : false
77
},
88
instance : {
9-
cache : true,
10-
cacheSaveCheck : true,
9+
identityCache : false,
10+
identityCacheSaveCheck : true,
1111
autoSave : false,
1212
autoFetch : false,
1313
autoFetchLimit : 1,

lib/Singleton.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ exports.clear = function (key) {
1010
};
1111

1212
exports.get = function (key, opts, createCb, returnCb) {
13-
if (opts && opts.cache === false) {
13+
if (opts && opts.identityCache === false) {
1414
return createCb(returnCb);
1515
}
1616
if (map.hasOwnProperty(key)) {
17-
if (opts && opts.save_check && typeof map[key].o.saved === "function" && !map[key].o.saved()) {
17+
if (opts && opts.saveCheck && typeof map[key].o.saved === "function" && !map[key].o.saved()) {
1818
// if not saved, don't return it, fetch original from db
1919
return createCb(returnCb);
2020
} else if (map[key].t !== null && map[key].t <= Date.now()) {
@@ -29,7 +29,7 @@ exports.get = function (key, opts, createCb, returnCb) {
2929

3030
map[key] = { // object , timeout
3131
o : value,
32-
t : (opts && typeof opts.cache === "number" ? Date.now() + (opts.cache * 1000) : null)
32+
t : (opts && typeof opts.identityCache === "number" ? Date.now() + (opts.identityCache * 1000) : null)
3333
};
3434
return returnCb(null, map[key].o);
3535
});

lib/TypeScript/orm.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ declare module "orm" {
190190
export class singleton {
191191
static clear(key?: string): singleton;
192192
static get(key, opts: {
193-
cache?: any;
194-
save_check?: boolean;
193+
identityCache?: any;
194+
saveCheck?: boolean;
195195
}, createCb: Function, returnCb: Function);
196196
}
197197

@@ -206,8 +206,8 @@ declare module "orm" {
206206
};
207207

208208
instance: {
209-
cache: boolean;
210-
cacheSaveCheck: boolean;
209+
identityCache: boolean;
210+
identityCacheSaveCheck: boolean;
211211
autoSave: boolean;
212212
autoFetch: boolean;
213213
autoFetchLimit: number;

0 commit comments

Comments
 (0)