memcached 不一樣客戶端的問題

摘要: memcached-java客戶端調用get方法獲取數據失敗css

主要演示一下在memcached服務器端set數據以後,在客戶端調用java api獲取數據。不過此過程若是不慎會讀取數據失敗。java

服務器端添加測試數據sql

set username 5 chencanjian STORED get username VALUE username 5 chencanjian END

表示在memcached服務器端獲取username的值是正常的api

memcached客戶端代碼以下:服務器

package com.memcached.util; import com.danga.MemCached.MemCachedClient; import com.danga.MemCached.SockIOPool; public class MemcachedUtil { protected static MemCachedClient mcc = new MemCachedClient(); static { String[] servers = {"192.168.0.100:11211"}; Integer[] weights = {3}; // grab an instance of our connection pool SockIOPool pool = SockIOPool.getInstance(); // set the servers and the weights pool.setServers(servers); pool.setWeights(weights); pool.setInitConn( 5 ); pool.setMinConn( 5 ); pool.setMaxConn( 250 ); pool.setMaxIdle( 1000 * 60 * 60 * 6 ); // set the sleep for the maint thread // it will wake up every x seconds and // maintain the pool size pool.setMaintSleep( 30 ); pool.setNagle( false ); pool.setSocketTO( 3000 ); pool.setSocketConnectTO( 0 ); // initialize the connection pool pool.initialize(); } public static void main(String[] args) { System.out.println(mcc.get("username")); } }

執行main方法後,出現了讀取異常,異常以下memcached

75 [main] ERROR com.danga.MemCached.MemCachedClient - ++++ exception thrown while trying to get object from cache for key: username 76 [main] ERROR com.danga.MemCached.MemCachedClient invalid stream header: 6C696C65 java.io.StreamCorruptedException: invalid stream header: 6C696C65 at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801) at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)

失敗的緣由爲:測試

在memcached中,不一樣的客戶端在set或者add值時,對命令的第二個參數的使用是不一致的 <command name> <key> <flags> <exptime> <bytes> <data block> JAVA客戶端flags字段填寫的都是32,不是32的是沒法經過java客戶端get出來的 因此在經過memcached服務器端進行數據set時,須要顯示指定flags值爲32 或者經過java客戶端提供的接口set數據
相關文章
相關標籤/搜索