java - springboot 如何基于注解方式集成 memcached?

 sense宏江 发布于 2022-10-26 14:56

最近想在springboot项目中集成memcached,网上查看的资料都是基于配置文件形式的,所以想看看如何基于注解方式来使用呢?还有一个问题就是

@Override
@ReadThroughSingleCache(namespace = "demo", expiration = 30000)
public Person selectById(Long id) {
    System.out.println("person 缓存未命中");
    return personMapper.selectById(id);
}

如何让 @ReadThroughSingleCache 注解生效呢?

1 个回答
  • <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>

    以上是官方的使用示例。


    导入配置 spring-boot Importing XML configuration

    通过@Bean定义对象 官方文档

    @Configuration
    public class AppConfig {
        @Bean
        public TransferService transferService() {
            return new TransferServiceImpl();
        }
    }
    2022-10-27 00:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有