Skip to content

Commit

Permalink
remove cache inside.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jul 10, 2024
1 parent 38fc0c6 commit ebe33d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fib-kv",
"version": "1.3.3",
"version": "1.4.0",
"description": "general key-value store on sql/level/mongo/redis for fibjs",
"main": "lib/index.js",
"types": "@types/index.d.ts",
Expand Down
43 changes: 6 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import backendUtils = require('./backend/_utils')
const backends = {
Object: require('./backend/Object'),
Map: require('./backend/Map'),
LruCache: require('./backend/LruCache'),
LevelDB: require('./backend/LevelDB'),
MongoDB: require('./backend/MongoDB'),
Redis: require('./backend/Redis'),
Expand All @@ -28,58 +27,36 @@ function backend(conn: FibKV.IConnection) {
return _back || (conn as any)._back || conn;
}

interface Cache extends Class_LruCache {
keys(): string[];
renew(k: string): any;
[k: string]: any
}
const FibKV = function (
this: FibKV.FibKVInstance,
conn: FibKV.IConnection | FibPoolNS.FibPool<FibKV.IConnection>, opts: FibKV.FibKVOptions = {}
) {
let cache: Cache;
if (opts.cache) {
cache = <Cache>new util.LruCache(utils.cache_size(opts), utils.cache_timeout(opts));
cache.keys = () => Object.keys(cache.toJSON());
cache.renew = (k) => cache.set(k, cache.get(k));
}

// fib-pool
if (typeof conn === 'function') {
const pool = conn as FibPoolNS.FibPool<FibKV.IConnection>
util.extend(this, {
setup: () => pool(utils.pool_name(opts), c => backend(c).setup(c, opts)),
get: k => pool(utils.pool_name(opts), c => {
k = utils.prefix_key(k, opts);
return cache ? cache.get(k, (k: string) => backend(c).get(c, opts, k)) :
backend(c).get(c, opts, k);
return backend(c).get(c, opts, k);
}),
set: (k, v) => pool(utils.pool_name(opts), c => {
k = utils.prefix_key(k, opts);
if (cache)
cache.set(k, v);
backend(c).set(c, opts, k, v);
}),
has: k => pool(utils.pool_name(opts), c => {
k = utils.prefix_key(k, opts);
return cache ? (cache.has(k) || backend(c).has(c, opts, k)) :
backend(c).has(c, opts, k);
return backend(c).has(c, opts, k);
}),
keys: () => pool(utils.pool_name(opts), c => backend(c).keys(c, opts)),
renew: k => pool(utils.pool_name(opts), c => {
k = utils.prefix_key(k, opts);
if (cache)
cache.renew(k);
backend(c).renew(c, opts, k);
}),
remove: k => pool(utils.pool_name(opts), c => {
k = utils.prefix_key(k, opts);
if (cache)
cache.remove(k);
backend(c).remove(c, opts, k);
}),
cache_has: k => cache && cache.has(utils.prefix_key(k, opts)),
cache_clear: () => cache && cache.clear()
})
} as FibKV.FibKVInstance);
} else {
const _back = backend(conn);
Expand All @@ -90,33 +67,25 @@ const FibKV = function (
},
get: k => {
k = utils.prefix_key(k, opts);
return cache ? cache.get(k, (k: string) => _back.get(conn, opts, k)) : _back.get(conn, opts, k);
return _back.get(conn, opts, k);
},
set: (k, v) => {
k = utils.prefix_key(k, opts);
if (cache)
cache.set(k, v);
_back.set(conn, opts, k, v);
},
has: k => {
k = utils.prefix_key(k, opts);
return cache ? (cache.has(k) || _back.has(conn, opts, k)) : _back.has(conn, opts, k);
return _back.has(conn, opts, k);
},
keys: () => _back.keys(conn, opts),
renew: k => {
k = utils.prefix_key(k, opts);
if (cache)
cache.renew(k);
_back.renew(conn, opts, k);
},
remove: k => {
k = utils.prefix_key(k, opts);
if (cache)
cache.remove(k);
_back.remove(conn, opts, k);
},
cache_has: k => cache && cache.has(utils.prefix_key(k, opts)),
cache_clear: () => cache && cache.clear()
}
} as FibKV.FibKVInstance);
}
} as any as FibKV.FibKVConstructor
Expand Down
10 changes: 1 addition & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,6 @@ describe("kv", () => {
() => conn = new collection.Map(),
() => conn = {});

test_kv('LruCache', {},
() => conn = new util.LruCache(65536),
() => conn = {});

test_timeout('LruCache timeout', {},
() => conn = new util.LruCache(65536, ms),
() => conn = {});

test_kv('LevelDB', {},
() => conn = db.openLevelDB("test.ldb"),
() => conn.close());
Expand Down Expand Up @@ -429,4 +421,4 @@ describe("kv", () => {
}
});

process.exit(test.run(console.DEBUG));
process.exit(test.run(console.DEBUG).failed);

0 comments on commit ebe33d2

Please sign in to comment.