php和java的memcached使用的兼容性问题解决过程

1. 背景:

  php 使用memcached客户端设置一个key,java使用java-memcached-client去读,报错如下:

ERROR|com.whalin.MemCached.MemCachedClient:-1|++++ exception thrown while trying to get object from cache for key: glt7hpcdi1ggo03l9qknu8a755

2. 网上搜索,发现最多的解释:

   在memcached中,不同的客户端在set或者add值时,对命令的第二个参数的使用是不一致的
    JAVA客户端flags字段填写的都是32,不是32的是无法通过java客户端get出来的所以在通过memcached admin进行数据set时,需要显示指定flags值为32set testkey 32 0 512345如此放入缓存后,通过java客户端是可以取出来的。
 
3. 可选的解决方案,1)从php端设置flag 2)从java端取的时候使用flag 3)使用别的客户端绕过这个障碍 。方案1,php端没有封装php的memcached客户端,调用在系统中随处可见,故该方案不可行,最起码风险比较大,成本高。方案2,研究了一下使用的memcached客户端,没法发现使用flag标示的地方,本身客户端如果自己去写的话,风险也不小。方案3,网上有人提到过

4.0. 增加spymemcached依赖

             net.spy spymemcached 2.12.0

4.1. 配置memcached的ip地址

   分别在dev,test,idc,prod的app-config.properties文件下添加memcached的ip地址:

   dev,test:memcache_ip=192.168.1.10:11211

  idc:memcache_ip=172.16.4.10:11211

 prod:memcache_ip=172.16.0.10:11211

4.2. 配置memcached实例:

app-cached.xml文件增加:

           

4.3. 新增类SpringContextHolder

import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;/** *  * 以静态变量保存Spring ApplicationContext,可在任何代码任何地方任何时候中取出ApplicaitonContext. **/public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; /** *  * 实现ApplicationContextAware接口的context注入函数,将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** *  * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /** *  * 从静态变量ApplicationContext中取得Bean,自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /** *  * 从静态变量ApplicationContext中取得Bean,自动转型为所赋值对象的类型. *  * 如果有多个Bean符合Class,取出第一个. */ @SuppressWarnings("unchecked") public static T getBean(Class clazz) { checkApplicationContext(); Map beanMaps = applicationContext.getBeansOfType(clazz); if (beanMaps != null && !beanMaps.isEmpty()) { return (T) beanMaps.values().iterator().next(); } else { return null; } } private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } }}

4.4.增加MemcacheUtil 

import com.test.bean.SpringContextHolder;import net.spy.memcached.MemcachedClient;public class MemcacheUtil {    public static MemcachedClient getMemCachedClient() {        return SpringContextHolder.getBean("memcachedClient");}}

4.5 测试代码

public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring-memcached-spy.xml"); MemcachedClient client=MemcacheUtil.getMemCachedClient();  //MemcachedClient client =(MemcachedClient)context.getBean("memcachedClient"); Object obj=client.get("glt7hpcdi1ggo03l9qknu8a755");  System.out.println(obj); }

注意事项:

使用spy memcached客户端,在key-value对中,如果value是对象的话,则读取不到,是string的话则可以读取。故建议使用时key-value约定为string类型。value为对象的话,可以考虑转成json串。

 

相关文章

前言设计一个缓存系统,不得不要考虑的问题就是:缓存穿透、...
在192.168.80.100要联网关闭防火墙及SElinuxsystemctlstopfi...
Redis与Memcache对比:1.Memcache是一个分布式的内存对象缓存...
安装Mencache:关闭防火墙及SElinuxsystemctl  stop  fi...
#安装php的yaf模块,参考https://www.cnblogs.com/shifu204/...
win10下安装配置apache、php、mysql、redis、memcache资源官...