消息关闭
    暂无新消息!
在学Spring的缓存使用。服务器上安装的redis。
然后在service层中使用@Cacheable 注解后,方法体中的查询就不执行了。
       写的是一部分接口,需要有返回参数跟信息,数据,所以封装成了一个对象。要缓存的不包括返回码,该怎么写?另 外dao层能不能直接用Spring的缓存注解,而不在service层中使用。

下面是代码,各位大佬麻烦看下:

@Service
@Transactional
public class PacketService implements IPacketService{
@Autowired
private PacketDao pDao;
@Autowired
private MermInfoDao mermDao;
@Autowired
private InviteDao inviteDao;

@Override
@Transactional(readOnly=true,propagation=Propagation.REQUIRED) //方法处的注解会覆盖类上的注解,用于特定的方法
@Cacheable("packetInfo")
public YJResult merPacketMsg(String merid,String merType, int startPage, int pageCount) {
try {
PageHelper.startPage(startPage, pageCount);
ArrayList<Packet> packetInfo = pDao.merPacketMsg(merid,merType);
return YJResult.ok(packetInfo);
} catch (RuntimeException e) {
e.printStackTrace();
return YJResult.build("001", "服务器异常");
}
}

@Override
@CachePut("packetInfo")
public YJResult mergePacket(double count, String merID) {
try {
double sum = mermDao.findPacketInfo(merID);
if(sum+count<0){
return YJResult.build("005", "红包金额不足");
}
pDao.updatePackInfo(count+sum,merID);
return YJResult.ok();
} catch (Exception e) {
e.printStackTrace();
return YJResult.build("001", "服务器异常");
}
}

@Override
@Transactional(propagation=Propagation.REQUIRED)
public YJResult donate(String sourceMerID, String targetMerTel, double count) {
try {
Commercial targetMerm = isAuthentication(targetMerTel);
Commercial sourceMerm = isAuthentication(sourceMerID);
if(targetMerm==null){
return YJResult.build("8888", "该商户未实名认证");
}
this.mergePacket(-count, sourceMerID);
//向对方待领取红包表插入记录
pDao.insertPackInfo(new Packet(targetMerm.getMerid(),count,sourceMerm.getMertype(),YJ.formatDate(new Date()),sourceMerID,sourceMerm.getMermp(),"1"));
//向自己的分享记录中插入数据
InviteLog inviteLog = new InviteLog(sourceMerID, targetMerm.getMermp(), YJ.formatDate(new Date()), YJ.formattime(new Date()), "I");
inviteDao.insertInviteInfo(inviteLog);
return YJResult.ok();
} catch (Exception e) {
e.printStackTrace();
return YJResult.build("001", "服务器异常");
}
}

@Override
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
@Cacheable("PhoneInfo")
public YJResult isPhoneRegist(String telNO) {
try {
boolean phoneCheck = false;
return YJResult.build("000", "Success", phoneCheck);
} catch (Exception e) {
e.printStackTrace();
return YJResult.build("001", "服务器异常");
}
}

@Override
@Transactional(propagation=Propagation.REQUIRED)
public YJResult withraw(double count, String merID) {
try {
this.mergePacket(-count, merID);
} catch (Exception e) {
e.printStackTrace();
return YJResult.build("002", "红包服务器异常");
}
return null;
}

@Override
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
@Cacheable("inviteInfo")
public YJResult inviteRecord(String merID, int startPage, int pageCount) {
try {
PageHelper.startPage(startPage, pageCount);
List<InviteLog> invites = inviteDao.findAllInvite(merID);
return YJResult.ok(invites);
} catch (Exception e) {
e.printStackTrace();
return YJResult.build("002", "服务器异常");
}
}

@Override
public int isMember(String merID) {
return 1;
}

@Override
public Commercial isAuthentication(String merid) {
// TODO Auto-generated method stub
return null;
}

}


Spring配置文件中缓存相关内容
<cache:annotation-driven />

<bean id="dataJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">

<property name="maxTotal" value="300" />
<!-- 最大空闲 -->
<property name="maxIdle" value="100" />
<!-- 最多等待毫秒 -->
<property name="maxWaitMillis" value="10000" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />

</bean>


此处是redis的 CacheManager
@Configuration
@EnableCaching
public class RedisCacheConfig extends CachingConfigurerSupport{
@Bean
public JedisConnectionFactory redisConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName("localhost");
factory.setPort(6379);
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate (RedisConnectionFactory rf){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(rf);
return template;
}

@Bean
public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setDefaultExpiration(3000);
return cacheManager;
}
/**
 * 自定义的缓存key生成策略:类名+方法名+参数名
 * @return
 */
@Bean
public KeyGenerator customerKeyGenerator(){
return new KeyGenerator() {

@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for(Object obj:params){
sb.append(obj.toString());
}
return sb.toString();
}
};
}
}


下面是执行查询的时候的日志
2017-06-26 00:03:38  [ http-bio-80-exec-1:17632 ] - [ DEBUG ]  Returning cached instance of singleton bean 'sqlSessionFactory'
2017-06-26 00:04:16  [ http-bio-80-exec-8:55155 ] - [ DEBUG ]  DispatcherServlet with name 'yjPacket' processing POST request for [/YJPacket-0.0.1/invite/packetInfo.do]
2017-06-26 00:04:16  [ http-bio-80-exec-8:55156 ] - [ DEBUG ]  Looking up handler method for path /invite/packetInfo.do
2017-06-26 00:04:16  [ http-bio-80-exec-8:55156 ] - [ DEBUG ]  Returning handler method [public cn.yj.common.YJResult cn.yj.controller.PacketController.merPacketMsg(java.lang.String,java.lang.String,int,int)]
2017-06-26 00:04:16  [ http-bio-80-exec-8:55157 ] - [ DEBUG ]  Returning cached instance of singleton bean 'packetController'
2017-06-26 00:04:16  [ http-bio-80-exec-8:55157 ] - [ DEBUG ]  Skip CORS processing: request is from same origin
2017-06-26 00:04:16  [ http-bio-80-exec-8:55159 ] - [ DEBUG ]  查询123321123商户的红包信息
2017-06-26 00:04:16  [ http-bio-80-exec-8:55159 ] - [ DEBUG ]  Creating new transaction with name [cn.yj.service.impl.PacketService.merPacketMsg]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2017-06-26 00:04:16  [ http-bio-80-exec-8:55159 ] - [ DEBUG ]  Acquired Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd] for JDBC transaction
2017-06-26 00:04:16  [ http-bio-80-exec-8:55160 ] - [ DEBUG ]  Setting JDBC Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd] read-only
2017-06-26 00:04:16  [ http-bio-80-exec-8:55161 ] - [ DEBUG ]  Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd] to manual commit
2017-06-26 00:04:16  [ http-bio-80-exec-8:55162 ] - [ DEBUG ]  Opening RedisConnection
2017-06-26 00:04:16  [ http-bio-80-exec-8:55162 ] - [ DEBUG ]  Closing Redis Connection
2017-06-26 00:04:16  [ http-bio-80-exec-8:55166 ] - [ DEBUG ]  Initiating transaction commit
2017-06-26 00:04:16  [ http-bio-80-exec-8:55166 ] - [ DEBUG ]  Committing JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd]
2017-06-26 00:04:16  [ http-bio-80-exec-8:55168 ] - [ DEBUG ]  Resetting read-only flag of JDBC Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd]
2017-06-26 00:04:16  [ http-bio-80-exec-8:55169 ] - [ DEBUG ]  Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6802b6dd] after transaction
2017-06-26 00:04:16  [ http-bio-80-exec-8:55169 ] - [ DEBUG ]  Returning JDBC Connection to DataSource
2017-06-26 00:04:16  [ http-bio-80-exec-8:55171 ] - [ DEBUG ]  Written [cn.yj.common.YJResult@6e7fdf7b] as "text/html;charset=UTF-8" using [com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@31668444]
2017-06-26 00:04:16  [ http-bio-80-exec-8:55171 ] - [ DEBUG ]  Null ModelAndView returned to DispatcherServlet with name 'yjPacket': assuming HandlerAdapter completed request handling
2017-06-26 00:04:16  [ http-bio-80-exec-8:55171 ] - [ DEBUG ]  Successfully completed request

1个回答