2012-05-27 70 views
3

我一直在嘗試瞭解memcached的用法等,並試圖從昨天開始通過閱讀少量稀缺資源來設置它。首先讓我展示一下我擁有的東西。嘗試實例化xmemcached客戶端

  1. 安裝memcached服務器
  2. 配置爲彈簧所解釋here

    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
    
        <import resource="simplesm-context.xml" /> 
        <aop:aspectj-autoproxy /> 
    
        <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"> 
          <property name="cacheClientFactory"> 
           <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /> 
          </property> 
          <property name="addressProvider"> 
           <bean class="com.google.code.ssm.config.DefaultAddressProvider"> 
            <property name="address" value="127.0.0.1:11211" /> 
           </bean> 
          </property> 
          <property name="configuration"> 
           <bean class="com.google.code.ssm.providers.CacheConfiguration"> 
             <property name="consistentHashing" value="true" /> 
           </bean> 
          </property> 
        </bean> 
    </beans> 
    
  3. 成功創建與服務器的連接槽彈簧:

    20:06:07,864 WARN [main] (XMemcachedClient.java:645) - XMemcachedClient use Text protocol 
    20:06:08,112 WARN [main] (AbstractController.java:372) - The Controller started at localhost/127.0.0.1:0 ... 
    20:06:08,139 WARN [Xmemcached-Reactor-0] (MemcachedConnector.java:239) - Add a session: 127.0.0.1:11211 
    

所以下一步是對其進行測試,這提供了良好/簡單的解釋有關的memcached:http://www.majordojo.com/2007/03/memcached-howto.php

我想嘗試該位:

Class Foo { 
    public static findById(id) { 
      if (obj = memcached.get(id)) return obj; 
      obj = loadFromDatabase(id); 
      memcached.put(id,obj); 
      return obj; 
     } 
} 

但遠不在這個網站沒有說哪個對象的類型是memcached。於是,我試着用以下:

import net.rubyeye.xmemcached.MemcachedClient 

Class Foo { 
     @Autowired 
    MemcachedClient defaultMemcachedClient; 
     public static findById(id) { 
       if (obj = memcached.get(id)) return obj; 
       obj = loadFromDatabase(id); 
       memcached.put(id,obj); 
       return obj; 
      } 
    } 

錯誤我從日誌中得到的是:

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.rubyeye.xmemcached.MemcachedClient] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency 

我想defaultMemcachedClient豆應該是我的memcached客戶端。這並不明顯。我在這裏做什麼?任何人有想法?鏈接?建議嗎?什麼?

回答

2

defaultMemcachedClient bean的類型爲Cache,與xmemecached無關。

如果要直接使用memcached,請不要使用Simple Spring Memcached。 SSM用於使用攔截器(AOP)和註釋進行緩存。你的情況:

public class Foo { 
    // cache for 1 hour 
    @ReadThroughSingleCache(namespace="foo", expiration=3600) 
    public Object findById(@ParameterValueKeyProvider int id) { 
      return loadFromDatabase(id); 
    } 
} 

當這個方法被調用此方法攔截檢查身體,如果有下鍵緩存中的所有值(關鍵是使用命名空間和ID參數創建)之前調用。如果緩存中有值,則返回給調用方,並且方法體(loadFromDatabase)不會被執行。如果執行方法的緩存體中沒有值,並且結果存儲在緩存中並返回給調用者。

如果您想直接使用memcached客戶端,您仍然可以使用SSM中的緩存對象,但您可能有興趣使用xmemcached

相關問題