memcached测试的行为不符合预期

蓝天

我正在实现https://code.google.com/p/simple-spring-memcached/,当我在下面的代码下运行时,收到以下输出:

Adding to cache
Adding to cache

测试memcached的代码:

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.code.ssm.CacheFactory;
import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ReadThroughSingleCache;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:memcached-context.xml" })
public class MemcachedTest {

    @Test
    public void testMemcached() {
        Assert.assertTrue(getComplexObjectFromDB("test").size() == 1);
        Assert.assertTrue(getComplexObjectFromDB("test").size() == 1);
    }

    private List<String> getSA(){

        List<String> l = new ArrayList<String>();
        System.out.println("Adding to cache");
        l.add("test");

        return l;
    }

    @ReadThroughSingleCache(namespace = "CplxObj", expiration = 3600)
    public List<String> getComplexObjectFromDB(@ParameterValueKeyProvider String id) {

        return getSA();
    }

    @Autowired
    private CacheFactory defaultMemcachedClient;

}

memcached-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />

    <context:annotation-config />

<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="xx.xx.xx.xx:11211" />
                </bean>
          </property>
          <property name="configuration">
                <bean class="com.google.code.ssm.providers.CacheConfiguration">
                      <property name="consistentHashing" value="true" />
                </bean>
          </property>
     </bean>

</beans>

我希望只收到一次输出“添加到缓存”,因为一旦将缓存添加到列表中就应该返回,并且不应该重新实例化列表?我的实现正确吗?

更新 :

根据@raznor的建议,这里的行为似乎与预期的一样:

memcached-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
   http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <mvc:annotation-driven />

    <context:annotation-config />

<import resource="simplesm-context.xml" />
  <aop:aspectj-autoproxy />

    <bean name="memcachedClient" class="com.memcached.MemcachedClient">
     </bean>

    <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="1.1.1.1:11211" />
                </bean>
          </property>
          <property name="configuration">
                <bean class="com.google.code.ssm.providers.CacheConfiguration">
                      <property name="consistentHashing" value="true" />
                </bean>
          </property>
     </bean>

</beans>

MemcachedTest.java:

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.code.ssm.CacheFactory;
import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ReadThroughSingleCache;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:memcached-context.xml" })
public class MemcachedTest {

    @Test
    public void testMemcached() {

        Assert.assertTrue(mClient.getComplexObjectFromDB("test1").size() == 1);
        Assert.assertTrue(mClient.getComplexObjectFromDB("test1").size() == 1);
    }

    @Autowired
    private MemcachedClient mClient;

}

MemcachedClient.java:

package com.memcached;

import java.util.ArrayList;
import java.util.List;

import com.google.code.ssm.api.ParameterValueKeyProvider;
import com.google.code.ssm.api.ReadThroughSingleCache;

public class MemcachedClient {

    @ReadThroughSingleCache(namespace = "CplxObj", expiration = 3600)
    public List<String> getComplexObjectFromDB(
            @ParameterValueKeyProvider String id) {

        return getSA();
    }

    private List<String> getSA() {

        List<String> l = new ArrayList<String>();
        System.out.println("Adding to cache");
        l.add("test");

        return l;
    }

}
拉格诺

它不起作用,因为未拦截自调用。为了使其工作,请提取getComplexObjectFromDB方法以分离Spring Bean,并在您的测试中使用以下bean对其进行调用:myBean.getComplexObjectFromDB。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

单元测试中的assertRaise行为不符合预期

来自分类Dev

Coffeescript类的行为不符合预期

来自分类Dev

SSLContext模拟行为不符合预期

来自分类Dev

类型n的行为不符合预期

来自分类Dev

CancellationTokenSource的行为不符合预期

来自分类Dev

WinWaitActive函数的行为不符合预期

来自分类Dev

变量的行为不符合预期

来自分类Dev

isinstance()的行为不符合我的预期

来自分类Dev

jQuery remove()行为不符合预期

来自分类Dev

Excel OR函数的行为不符合预期

来自分类Dev

强行“替换”行为不符合预期

来自分类Dev

reactValuesToList行为不符合预期

来自分类Dev

CancellationTokenSource的行为不符合预期

来自分类Dev

类型n的行为不符合预期

来自分类Dev

Coffeescript类的行为不符合预期

来自分类Dev

角度`watch`的行为不符合预期

来自分类Dev

primaryValues的行为不符合预期

来自分类Dev

autocmd行为不符合预期

来自分类Dev

while循环的行为不符合预期?

来自分类Dev

WinWaitActive函数的行为不符合预期

来自分类Dev

VBA词典的行为不符合预期

来自分类Dev

HashMap的行为不符合汉字的预期

来自分类Dev

MySQL查询的行为不符合预期

来自分类Dev

C ++指针的行为不符合预期

来自分类Dev

while(f) 行为不符合预期

来自分类Dev

Numpy 数组的行为不符合预期

来自分类Dev

Zip 的行为不符合预期

来自分类Dev

PowerShell - 返回行为不符合预期

来自分类Dev

const引用的指针行为不符合预期