跳轉至內容

XQuery/使用Memcached模組

來自華夏公益教科書,開放書籍,開放世界

您希望設定一個分散式快取系統,以便多個系統可以共享有關它們各自快取中內容的資訊。此模組實現了memcached協議。

注意:我在eXist 2.1下使用memcached模組時遇到了一些問題。如果您能使其正常工作,請告訴我。

要啟用該模組,您必須執行以下步驟

步驟 1 將 $EXIST_HOME/extensions/local.build.properties 中的 include.module.memcached 屬性從 false 更改為 true

  include.module.memcached = true

步驟 2 取消 $EXIST_HOME/conf.xml 中的註釋

  <module uri="http://exist-db.org/xquery/memcached" class="org.exist.xquery.modules.memcached.MemcachedModule" />

步驟 3

  • 執行“build”重新編譯

步驟 4

  • 重啟eXist伺服器

步驟 5

  • 啟動您的memcached伺服器(它不是eXist系統的一部分)

測試模組是否已載入

[編輯 | 編輯原始碼]

如果您執行函式 util:get-module-info('http://exist-db.org/xquery/memcached'),它現在應該返回模組描述。

函式概要

[編輯 | 編輯原始碼]

用於建立和關閉memcache客戶端的函式

[編輯 | 編輯原始碼]

有兩個函式可以建立和刪除客戶端:mcache:create-client($properties, $is-binary-indicator)mcache:shutdown($client)

函式 mcache:create-client 返回一個 xs:long,表示客戶端控制代碼。所有將來的引用都將使用此控制代碼。

用於設定和獲取鍵/值對的函式

[編輯 | 編輯原始碼]

示例程式碼

[編輯 | 編輯原始碼]

以下示例程式碼由模組作者 Evgeny Gazdovsky 提供

建立Memcache客戶端

[編輯 | 編輯原始碼]

在您需要為一個或多個memcached伺服器建立memcached客戶端之前。

import module namespace cache="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

let $properties := <properties><property name="host" value="localhost"/></properties>

let $client := mcache:create-client($properties, false())

return cache:put("memcached", "client", $client)

新增多個伺服器

[編輯 | 編輯原始碼]

如果您希望使用一個客戶端連線多個伺服器,則必須新增屬性,例如

let $properties :=
( 
   <properties><property name="host" value="server1"/></properties>,
   <properties><property name="host" value="server2"/></properties>,
   <properties><property name="host" value="server3"/></properties>
)

更改Memcached埠

[編輯 | 編輯原始碼]

如果您的memcached伺服器偵聽的埠不是標準埠:11211,則必須為該伺服器新增“port”屬性

let $properties :=
<properties>
   <property name="host" value="localhost"/>
   <property name="port" value="your port value here"/>
</properties>

在Memcached中設定值

[編輯 | 編輯原始碼]

為了在不同的查詢中使用相同的連線,我們使用快取模組來儲存客戶端控制代碼。如果您希望在memcached中設定(新增、替換、刪除)內容,可以使用類似以下的指令碼

import module namespace cache="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

let $client := cache:get("memcached", "client")

return
   if ($client)
      then mcache:set($client, "key", "foo", 3600)
      else ()

此指令碼將為鍵“key”儲存字串值“foo”,持續時間為 3600 秒。除了 xs:base64Binary 之外的所有值都將儲存為字串。xs:base64Binary 將儲存為位元組陣列。要儲存 XML 片段,您必須先對其進行序列化(請參閱 util:serialeze() 函式)。要從 memcached 中獲取資料,請使用類似以下的指令碼

import module namespace cache="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

let $client := cache:get("memcached", "client")

return
   if ($client)
      then mcache:get($client, "key")
      else ()

經過一段時間(本例中為 3600 秒)後,該值將過期,之後,將返回空序列以表示鍵“key”。

當您進行連線時,您應該能夠在控制檯中看到以下 INFO 訊息

  2012-03-10 11:16:15.741 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=localhost/127.0.0.1:11211,
  #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue

Rops 是讀取操作,Wops 是寫入運算元量。

 2012-03-10 11:16:15.819 INFO net.spy.memcached.MemcachedConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@66a236

關閉時,日誌檔案中將包含以下內容

 2012-03-10 11:55:42.850 INFO net.spy.memcached.MemcachedClient:  Shut down memcached client

參考文獻

[編輯 | 編輯原始碼]

在Windows上設定memcached

華夏公益教科書