日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

设置log缓存_node多级缓存之redis缓存

發(fā)布時間:2024/8/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设置log缓存_node多级缓存之redis缓存 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在node項目開發(fā)過程中,緩存常常被用來解決高性能、高并發(fā)等問題。在我們的實際項目中,運用緩存的思路是內(nèi)存緩存-->接口-->文件緩存。前面的總結(jié)中已經(jīng)詳細(xì)的說明了怎么實現(xiàn)和封裝內(nèi)存緩存和文件緩存。雖然二級緩存已經(jīng)基本能夠滿足現(xiàn)在的所有場景需求,但現(xiàn)在我們再加一級redis緩存,從而使我們的項目更加穩(wěn)定。

redis的封裝:

const?redis?=?require('redis');const myredis = { client:null, connect: function () { let RDS_PORT = 6379, //端口號??????RDS_HOST?=?'94.191.**.**',????//服務(wù)器IP RDS_PWD = '**********', //密碼 RDS_OPTS = {}; //設(shè)置項 this.client = redis.createClient(RDS_PORT, RDS_HOST, RDS_OPTS); let c: any = this.client; c.auth(RDS_PWD, function () { console.log('通過認(rèn)證'); }); c.on('error', function (err:any) { console.log('redisCache Error ' + err); }); c.on('ready', function () { console.log('redisCache connection succeed'); }); }, init: function () { this.connect(); // 創(chuàng)建連接 const instance:any = this.client; // 主要重寫了一下三個方法。可以根據(jù)需要定義。 const get = instance.get; const set = instance.set; const setex = instance.setex; instance.set = function (key:any, value:any, callback:any) { if (value !== undefined) { set.call(instance, key, JSON.stringify(value), callback); } }; instance.get = function (key: any, callback: any) { return new Promise(function(resolve,reject){ get.call(instance, key, (err: any, val: any) => { resolve(JSON.parse(val)) }) }) }; instance.setex = function (key: any, value: any, callback: any) { if (value !== undefined) { setex.call(instance, key, 30*60*1000, JSON.stringify(value), callback); } }; return instance; },};export default myredis.init();

為了滿足更多場景的使用,我們這里對redis的set和get方法進(jìn)行了重寫,并在結(jié)尾導(dǎo)出了新的redisclient實例?instance?,在get 方法中,為了在外部調(diào)用的過程中直接拿到數(shù)據(jù),我們使用了 Promise 封裝了一下get方法。

使用:

.............import?rediscache?from?"../redis";.............async function apiCache(input_options: apicache_options): Promise<any> {?............ let key: string if (options.keystr != '') { key = encryption.sha1('apicache:' + options.keystr) } else if (options.postdata != null) { key = encryption.sha1('apicache:' + options.url + ';data:' + object_hash(options.postdata)) } else { key = encryption.sha1('apicache:' + options.url) //根據(jù)頁面路徑生成key??} //redis緩存??let?redis_cache?=?await?rediscache.get(key);??if?(redis_cache?!=?null){ return redis_cache; }; //內(nèi)存緩存??let?api_memery_cache?=?memorycache.get(key)??if?(api_memery_cache?!=?null)?{ return api_memery_cache.body } else { //文件緩存 let api_file_cache = await filecache.get(key) //嘗試文件緩存 // console.log(api_file_cache) if (api_file_cache != null) { // console.info('走文件')??????memorycache.put(key,?api_file_cache)?//寫入內(nèi)存 refreshCache(key, api_file_cache.time, options) return api_file_cache.body } } // console.info('直接調(diào)接口') try { let aoptions: any = { method: options.method, url: options.url, timeout: options.timeout, data: options.postdata, headers: options.headers } if (options.is_stream) { aoptions.responseType = 'arraybuffer' aoptions.responseEncoding = 'binary' } let back = await axios(aoptions) // console.log(back) let backdata = back.data; if (options.error_replace && options.check_callback) { if (options.check_callback(backdata)) { } else { logger.error({ 'error_message': '接口返回數(shù)據(jù)驗證失敗', 'url': options.url }) backdata = options.error_replace } } let cache_content = { time: Date.now(), body: backdata }????rediscache.set(key,?cache_content)???? memorycache.put(key, cache_content) filecache.put(key, cache_content) if (options.success_log) { logger.info({ url: options.url, back: backdata }) } return backdata } catch (error) { logger.error({ 'error_message': error.message, 'url': options.url }) if (options.error_replace !== undefined) { return options.error_replace } else { throw error } }}export default apiCache

完整的緩存中間件就不贅述,可看以前的總結(jié)web建站小結(jié)?,在上面的刪減版的代碼中,為了方便觀察,將redis緩存的讀寫放在了最上面,啟動項目,在redis桌面管理工具(RedisDesktopManager)中查看:

頁面上對應(yīng)的部分:

總結(jié)

以上是生活随笔為你收集整理的设置log缓存_node多级缓存之redis缓存的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。