鯤鵬網頁緩存庫:KPCache

jopen 10年前發布 | 8K 次閱讀 KPCache 緩存組件

鯤鵬網頁緩存庫(Python) - KPCache

特性: 無需后端數據庫,直接操作文件系統(建議使用Ext4文件系統),速度快。 基于ZMQ通信,支持網絡高速存取。 支持多線程。 支持服務端掉線自動重連。 支持多種緩存數據類型(所有能被Python pickle的類型均被支持,不限于字符串)。 完全兼容webscraping庫。

實測速度: 存儲100萬文檔(單個大小為6.5KB)用時約1000秒。測試腳本:test.py。

安裝依賴庫ZMQ: sudo easy_install zmq

啟動服務端: >>> from KPCache import KPCache >>> KPCache.KPCacheServer(server_host='127.0.0.1', server_port=8080, folder='.KPCache').run_server() The cache server is running on 127.0.0.1:8080, cache data will be saved in .KPCache

server_host - 服務端監聽的地址,默認是127.0.0.1,如果想遠程使用,可以設置為0.0.0.0。 server_port - 服務端監聽的端口,默認是8080。 folder - 緩存數據的存儲目錄,默認是.KPCache目錄。

客戶端使用舉例: >>> from KPCache import KPCache >>> cache = KPCache.KPCacheClient(server_host='127.0.0.1', server_port=8080) >>> >>> cache['name'] = 'Qi' # 測試保存一個字符串 >>> print cache['name'] # 測試讀取 Qi >>> >>> cache['dict'] = {'website': ' # 測試保存一個字典類型數據 >>> print cache['dict'] {'website': ' >>> >>> from datetime import datetime >>> cache['date_time'] = datetime.now() # 測試保存一個時間日期類型的數據 >>> print cache['date_time'] 2014-01-23 18:34:15.921058 >>> >>> del cache['dict'] # 測試刪除某個緩存 >>> print cache['dict'] # 讀取不存在的緩存會拋出 KeyError異常 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "KPCache/KPCache.py", line 58, in getitem raise KeyError('%s does not exist' % key) KeyError: 'dict does not exist' >>> >>> '

注意: 在多線程中使用時,每個線程需要建立各自客戶端實例,不能共享同一個客戶端實例。</pre>https://bitbucket.org/qi/kpcache/overview

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!