I’ve been trying to learn about memcached usage etc. and trying to set it up from yesterday by reading few scarce resources out there. Let me begin by showing what I have.
- Installed memcached server
-
Configured for spring as explained 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> -
Successfully created connection to the server trough spring :
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
So next step was to test it, this offers good/simple explanation about memcached : http://www.majordojo.com/2007/03/memcached-howto.php
I wanted to try this bit :
Class Foo {
public static findById(id) {
if (obj = memcached.get(id)) return obj;
obj = loadFromDatabase(id);
memcached.put(id,obj);
return obj;
}
}
But nowhere on this site does it say which type of object is memcached. So I tried with following :
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;
}
}
Error I got from the logs is :
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
I thought defaultMemcachedClient bean is supposed to be my memcached client. It’s not obviously. What do I do here? Anyone has idea? Link? Advice? Anything?
The defaultMemcachedClient bean is of type Cache and has nothing to do with xmemecached.
If you want to use memcached directly don’t use Simple Spring Memcached. The SSM is for caching using interceptors (AOP) and annotation. In your case:
When this method is called, before invoking body of this method interceptor check if there’s any value in cache under key (key is created using namespace and id parameter). If there’s a value in cache it is returned to caller and body of method (loadFromDatabase) is not executed. If there’s no value in cache body of method is executed and result is stored in cache and returned to caller.
If you want to use memcached client directly you can still use Cache object from SSM but you may be interested in using xmemcached.