最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js 實(shí)現(xiàn)簡(jiǎn)單的無(wú)侵入式緩存框架的方法

 更新時(shí)間:2019年07月21日 10:11:57   作者:Scott Lee  
這篇文章主要介紹了Node.js 實(shí)現(xiàn)簡(jiǎn)單的無(wú)侵入式緩存框架的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

python 的flask.ext.cache 通過(guò)注解這樣對(duì)方法返回結(jié)果進(jìn)行緩存:

@cache.cached(timeout=300, key_prefix='view_%s', unless=None)
def hello(name=None):
  print 'view hello called'
  return render_template('hello.html', name=name)

這類(lèi)實(shí)現(xiàn)方式對(duì)業(yè)務(wù)邏輯沒(méi)有絲毫的侵入性,非常之優(yōu)雅。

最近在做 Node.js 地項(xiàng)目,然而 js ES 7 之前都不支持注解,目前見(jiàn)到的緩存框架雖然在 API 設(shè)計(jì)上都很簡(jiǎn)潔、很有想法。

可是痛點(diǎn)在于它們都是侵入式的,需要在業(yè)務(wù)邏輯代碼中插入緩存邏輯,這些方式很不優(yōu)雅。

正題

今天花點(diǎn)時(shí)間研究下js有沒(méi)有辦法,以比較優(yōu)雅地方法實(shí)現(xiàn)緩存。

我對(duì)緩存框架的訴求:

  • 不對(duì)原方法進(jìn)行更改
  • 能實(shí)現(xiàn)對(duì)不同參數(shù)地緩存
  • 支持緩存時(shí)間

我了解到的 js 能力:

  1. 隱藏參數(shù)arguments可以獲取參數(shù)列表
  2. prototype 可用來(lái)重寫(xiě)覆蓋原方法

可行性?

看了看 prototype 文檔

直覺(jué)告訴我看起來(lái)可行,以下是官方的說(shuō)明:

當(dāng)一個(gè)函數(shù)被調(diào)用時(shí),調(diào)用的參數(shù)被保留在類(lèi)似數(shù)組 "變量" 的參數(shù)中。例如, 在調(diào)用 "myFn (a、b、c)"時(shí), 在myFn 的主體內(nèi)的參數(shù)將包含 3個(gè)類(lèi)似數(shù)組的元素對(duì)應(yīng)于 (a、b、c)。 使用鉤子修改原型時(shí),只需通過(guò)調(diào)用該函數(shù)的 apply (),將 this 與參數(shù) (調(diào)用狀態(tài)) 傳遞給當(dāng)前行為。這種模式可以用于任何原型,如 Node.prototype、 Function.prototype 等.

var current = Object.prototype.valueOf;
// 由于我的屬性 "-prop-value"是交叉性的, 并不總是
// 在同一個(gè)原型鏈上,我想要修改 Object.prototype: 
Object.prototype.valueOf = function() {
 if (this.hasOwnProperty('-prop-value')) {
  return this['-prop-value'];
 } else {
  // 它看起來(lái)不像我的對(duì)象之一,因此,讓我們退回到 
  // 默認(rèn)行為,通過(guò)盡可能地復(fù)制當(dāng)前行為來(lái)實(shí)現(xiàn).
  // 此apply的行為類(lèi)似于其他語(yǔ)言中的"super".
  // 即使 valueOf() 不帶參數(shù), 其他的鉤子可能會(huì)帶有.
  return current.apply(this, arguments);
 }
}

從示例不難看出,我可以在某些條件下通過(guò) apply() 方法調(diào)用函數(shù)原邏輯,某些條件執(zhí)行我需要的新邏輯。

寫(xiě)個(gè) demo 測(cè)試一下

// 重寫(xiě)Function的原型方法cache
Function.prototype.cache = function () {
  var _self = this;
  return function() {
 console.log('arguments', arguments);
 var key = arguments[0];
    if (cache.has(key)) {
      return cache.get(key)
    } else {
      return _self.apply(this, arguments)
    }
  }
}

定義 cache,當(dāng)且僅當(dāng) key 為 1 時(shí)有值

var cache = {
  has: (key) => {
 if (key === 1) return true
 else return false
  },
  get: (key) => {
    return "cached value " + key
  }
}

定義測(cè)試方法

function request(key) {
 return 'value of ' + key
}

應(yīng)用注入

request = request.cache()

執(zhí)行一下

request(2)
"value of 2"
request(1)
"cached value 1"

看到結(jié)果按照預(yù)期輸出,完美!

最后實(shí)現(xiàn)

項(xiàng)目引用了 memory-cache 作為基礎(chǔ)緩存庫(kù),實(shí)現(xiàn)了相關(guān)的緩存功能。

simple-cache.js
const cache = require('memory-cache');
Function.prototype.cache = function (cachekey, time) {
  var _self = this;
  return function() {
 var key = cachekey(arguments);
    var value = cache.get(key);
    if (!value) {
      value = _self.apply(this, arguments)
  cache.put(key, value, time);
    }
    return value;
  }
}
var simpleCache = {
 cache: function(f, cacheKey, cacheTime) {
 return f.cache(cacheKey, cacheTime);
 }
}
module.exports = simpleCache
sample.js
const cache = require('simple-cache-z').cache;
function cachekey(args) {
  return args[0]
}
function request(key) {
  return (new Date()).getTime();
}
request = cache(request, cachekey, 5000);
console.log('request 1 ', request(1));
setTimeout(() => {
  console.log('request 2 ', request(2));
}, 1000)
setTimeout(()=> {
  console.log('request 1 ', request(1))
  console.log('request 1 ', request(1))
  console.log('request 1 ', request(1))
  console.log('request 2 ', request(2));
  console.log('request 2 ', request(2));
  console.log('request 2 ', request(2));
}, 2000);
setTimeout(()=> {
  console.log('request 1 ', request(1));
  console.log('request 1 ', request(1));
  console.log('request 1 ', request(1));
  console.log('request 2 ', request(2));
  console.log('request 2 ', request(2));
  console.log('request 2 ', request(2));
}, 10000);

輸出結(jié)果

request 1  1563000551142
// 1000 ms
request 2  1563000552150
// 2000 ms
request 1  1563000551142
request 1  1563000551142
request 1  1563000551142
request 2  1563000552150
request 2  1563000552150
request 2  1563000552150
// 10000 ms
request 1  1563000561151
request 1  1563000561151
request 1  1563000561151
request 2  1563000561151
request 2  1563000561151
request 2  1563000561151

大功告成!

今日研究成果

事實(shí)證明方案可行,應(yīng)用到我的項(xiàng)目中對(duì)執(zhí)行效率和代碼可讀性的提升非常明顯。

我已經(jīng)把框架打成了包,上傳到 npm 倉(cāng)庫(kù) simple-cache-z ,可通過(guò)如下方式引用。

npm install --save simple-cache-z

用法和代碼上傳至 github 倉(cāng)庫(kù),歡迎提交代碼和 star:

https://github.com/auv1107/simple-cache-nodejs

總結(jié)

以上所述是小編給大家介紹的Node.js 實(shí)現(xiàn)簡(jiǎn)單的無(wú)侵入式緩存框架的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論

秀山| 乌恰县| 洞头县| 巴马| 基隆市| 光泽县| 楚雄市| 乌恰县| 仙居县| 周宁县| 乌兰察布市| 成武县| 遵义县| 东明县| 临安市| 古交市| 鲁甸县| 芮城县| 东安县| 霍林郭勒市| 开阳县| 淳安县| 西昌市| 文安县| 武鸣县| 新安县| 城市| 渭源县| 利辛县| 黄浦区| 南江县| 右玉县| 嵊泗县| 广河县| 饶阳县| 南澳县| 南皮县| 庆云县| 通辽市| 怀宁县| 巴青县|