Skip to content

Commit

Permalink
fix cache error. (#46)
Browse files Browse the repository at this point in the history
* fix cache error.

* feat: augument type for fib-cache.

---------

Co-authored-by: richardo2016 <[email protected]>
  • Loading branch information
xicilion and richardo2016 authored Jul 12, 2024
1 parent a8a328b commit a6884b0
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/orm/src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { FxOrmSqlDDLSync } from '@fxjs/sql-ddl-sync';
import { FxOrmDMLDriver } from './Typo/DMLDriver';
import { FxOrmError } from './Typo/Error';
import { FxOrmQuery } from './Typo/query';
const fc = require('fib-cache');
import fc = require('fib-cache');

import type {
FxSqlQuerySubQuery,
Expand Down
46 changes: 46 additions & 0 deletions packages/orm/src/Patch/fib-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fibCache = require('fib-cache');


declare module "fib-cache" {
interface LRUOptions {
max?: number;
ttl?: number;
resolver?: (key: string) => any;
}

interface LRUItem {
expiry: number;
key: string;
prev: LRUItem;
next: LRUItem;
owner: LRU;
value: any;
ready: Class_Event;
}

export class LRU {
constructor(options?: LRUOptions);
items: Record<string, LRUItem>;
size: number;
first: LRUItem;
last: LRUItem;
max: number;
ttl: number;
resolver: (key: string) => any;

clear(): LRU;
delete_item(item: LRUItem): LRU;
delete(key: string): LRU;
entries(keys?: string[]): [string, any][];
evict(): LRU;
get(key: string): any;
has(key: string): boolean;
keys(): string[];
new_item(key: string, value?: any): LRUItem;
order_item(item: LRUItem): void;
set(key: string, value: any): LRU;
values(keys?: string[]): any[];
}
}

module.exports = fibCache;
31 changes: 16 additions & 15 deletions packages/orm/src/Singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const clear: FxOrmNS.SingletonModule['clear'] = function (key?: string) {

export const modelClear: FxOrmNS.SingletonModule['modelClear'] = function (model: FxOrmModel.Model, key?: string) {
if (key && typeof key === "string") {
model.caches.remove(key);
model.caches.delete(key);
} else {
model.caches.clear();
}
Expand All @@ -45,20 +45,21 @@ export const modelGet: FxOrmNS.SingletonModule['modelGet'] = function (model, ke
if (opts.identityCache === false)
return reFetchSync();

if (model.caches.has(key))
if (opts && opts.saveCheck && typeof model.caches.get(key).saved === "function" && !model.caches.get(key).saved())
var value = model.caches.get(key);

if (value !== undefined)
{
if (opts && opts.saveCheck && typeof value.saved === "function" && !value.saved())
// if not saved, don't return it, fetch original from db
return reFetchSync();

const value = model.caches.get(key, function (_key: string) {
const new_value = reFetchSync();
model.caches.set(_key, new_value);

return new_value;
});

const expected_expire = typeof opts.identityCache === "number" ? (opts.identityCache * 1000) : model.caches.timeout;
const expire_expection_delta = expected_expire - model.caches.timeout;
}else
{
value = reFetchSync();
model.caches.set(key, value);
}

const expected_expire = typeof opts.identityCache === "number" ? (opts.identityCache * 1000) : model.caches.ttl;
const expire_expection_delta = expected_expire - model.caches.ttl;

if (expire_expection_delta > 0) {
coroutine.start(() => {
Expand All @@ -69,14 +70,14 @@ export const modelGet: FxOrmNS.SingletonModule['modelGet'] = function (model, ke

model.caches.set(key, value);
if (expected_expire > Date.now()) {
model.caches.remove(key)
model.caches.delete(key)
break
}
}
});

} else if (expire_expection_delta < 0) {
setTimeout(() => model.caches.remove(key), expected_expire);
setTimeout(() => model.caches.delete(key), expected_expire);
}

return value;
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/src/Typo/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export namespace FxOrmModel {
/* @nonenum */
uid: string;

caches: Class_LruCache;
caches: import('fib-cache').LRU;

keys: string[];

Expand Down
2 changes: 2 additions & 0 deletions packages/orm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { ORM } from './ORM';

export * as Property from "./Property";

import './Patch/fib-cache';

function isOrmLikeErrorEmitter(parsedDBDriver: IDbDriver | FxOrmNS.ORMLike): parsedDBDriver is FxOrmNS.ORMLike {
return !parsedDBDriver.hasOwnProperty('host') && parsedDBDriver instanceof events.EventEmitter
}
Expand Down
40 changes: 40 additions & 0 deletions packages/orm/typings/Patch/fib-cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference types="@fibjs/types" />
declare const fibCache: any;
declare module "fib-cache" {
interface LRUOptions {
max?: number;
ttl?: number;
resolver?: (key: string) => any;
}
interface LRUItem {
expiry: number;
key: string;
prev: LRUItem;
next: LRUItem;
owner: LRU;
value: any;
ready: Class_Event;
}
class LRU {
constructor(options?: LRUOptions);
items: Record<string, LRUItem>;
size: number;
first: LRUItem;
last: LRUItem;
max: number;
ttl: number;
resolver: (key: string) => any;
clear(): LRU;
delete_item(item: LRUItem): LRU;
delete(key: string): LRU;
entries(keys?: string[]): [string, any][];
evict(): LRU;
get(key: string): any;
has(key: string): boolean;
keys(): string[];
new_item(key: string, value?: any): LRUItem;
order_item(item: LRUItem): void;
set(key: string, value: any): LRU;
values(keys?: string[]): any[];
}
}
3 changes: 1 addition & 2 deletions packages/orm/typings/Typo/model.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/// <reference types="@fibjs/types" />
/// <reference types="@fibjs/enforce" />
/// <reference types="@fibjs/types" />
import type { FxOrmSqlDDLSync } from "@fxjs/sql-ddl-sync";
Expand Down Expand Up @@ -28,7 +27,7 @@ export declare namespace FxOrmModel {
table: string;
id: string[];
uid: string;
caches: Class_LruCache;
caches: import('fib-cache').LRU;
keys: string[];
allProperties: Record<string, FxOrmProperty.NormalizedProperty>;
virtualProperties: Record<string, FxOrmProperty.NormalizedProperty>;
Expand Down
1 change: 1 addition & 0 deletions packages/orm/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Helpers from "./Helpers";
import * as singleton from "./Singleton";
import { ORM } from './ORM';
export * as Property from "./Property";
import './Patch/fib-cache';
export declare function connectSync(opts?: string | FxDbDriverNS.DBConnectionConfig): ORM;
export declare function connect<T extends IDbDriver.ISQLConn = any>(uri?: string | FxDbDriverNS.DBConnectionConfig, cb?: FxOrmCoreCallbackNS.ExecutionCallback<IDbDriver<T>>): ORM;
export { ORM } from './ORM';
Expand Down

0 comments on commit a6884b0

Please sign in to comment.