在spring框架里把数据存到Redis中失败了,但是写个小demo又是成功的

这个是运行的代码

@Autowired
        private RedisTemplate<String,Object> redisTemplate;

        @Override
        public List<TbContent> findByCategoryId(Long categoryId) {
            //首先从redis中取数据
            List<TbContent> list = (List<TbContent>)redisTemplate.boundHashOps("guanggao").get(categoryId);
            //1.取到了直接返回,不走数据库
            if(list != null && list.size() > 0){
                System.out.println("直接走reids,不去查询数据库了:" + list);
                return list;
            }
            //2.取不到,走数据库
            TbContentExample example=new TbContentExample();
            example.setOrderByClause("sort_order desc");//通过降序排列
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);//根据分类id查询
            //只查询state是1的表示启用的广告
            criteria.andStatusEqualTo("1");//开启的广告
            list = contentMapper.selectByExample(example);
            //查询了数据库后,将数据放入到redis,下次就不走数据库了
            redisTemplate.boundHashOps("guanggao").put(categoryId, list);
            System.out.println("走数据库,并且存入到reids中:" + list);
            return list;
        }

这个是日志的结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@591987d4] was not registered for synchronization because synchronization is not active
直接走reids,不去查询数据库了:[com.jd.pojo.TbContent@3f0fdfb8, com.jd.pojo.TbContent@7a0d463d, com.jd.pojo.TbContent@72d4fc40]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca4e929] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.jdbc.JDBC4Connection@489b7465] will not be managed by Spring
JDBC Connection [com.mysql.jdbc.JDBC4Connection@397574e] will not be managed by Spring
==>  Preparing: select id, category_id, title, url, pic, status, sort_order from tb_content WHERE ( category_id = ? and status = ? ) order by sort_order desc 
==>  Preparing: select id, category_id, title, url, pic, status, sort_order from tb_content WHERE ( category_id = ? and status = ? ) order by sort_order desc 
==> Parameters: 6(Long), 1(String)
==> Parameters: 6(Long), 1(String)
<==      Total: 0
<==      Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@591987d4]
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca4e929]
走数据库,并且存入到reids中:[]
走数据库,并且存入到reids中:[]

希望大佬能帮忙看看问题在哪里?

3个回答

TXT__
TXT__ 哥,我这个具体怎么解决还事不清楚啊
5 个月之前 回复

你这应该不是方redis失败了,是在数据库没查询到数据

qq_36601979
zyydomain 回复TXT__: 如果个某个idredis没有,会去查数据库,web端显示的是你redis之前存在的吧?如果redis没有就去查数据库,而事务没被spring管理,查不到数据,看下你的配置文件;
5 个月之前 回复
TXT__
TXT__ 但是web端页面显示正常的
5 个月之前 回复

这是我连redis的操作,有用望采纳:

package com.per.mybootall.service.impl;

import com.google.gson.Gson;
import com.per.mybootall.dao.ProductMapper;
import com.per.mybootall.pojo.Product;
import com.per.mybootall.service.HomeLoadInter;
import com.per.mybootall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

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

@Service
public class HomeLoadimpl implements HomeLoadInter {
    @Autowired
    private ProductMapper productmapper;
//    @Autowired
//    private  ProductVO proVO;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private Product pro;
    @Override
    public List<ProductVO> selectProductMessage(){
        //字符串的序列化器
        RedisSerializer redisSerializer=new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        List<ProductVO> listredis=new ArrayList<ProductVO>();
         listredis=  (List<ProductVO>)redisTemplate.opsForValue().get("HomeLoadMassage");

        if(null==listredis){
            synchronized (this){
                 listredis=  (List<ProductVO>)redisTemplate.opsForValue().get("HomeLoadMassage");
                if(null==listredis){
                    System.out.println("缓存咩有的时候:"+listredis);
                    List<ProductVO> listrediss=new ArrayList<ProductVO>();

                    for(long i=1;i<4;i++){
                        ProductVO proVO=new ProductVO();
                         pro= productmapper.selectByPrimaryKey(i);
                        proVO.setName(pro.getName());
                        proVO.setSubtitle(pro.getSubtitle());
                        proVO.setMainImage(pro.getMainImage());
                        proVO.setSubImage(pro.getSubImage());
                        proVO.setPrick(pro.getPrick());
                        proVO.setStock(pro.getStock());
                        System.out.println(proVO.toString());

                       listrediss.add(proVO);
                        System.out.println("listredis:"+listrediss);
                    }
                    System.out.println("all:"+listrediss);
                    redisTemplate.opsForValue().set("HomeLoadMassage",listrediss);
                    return listrediss;
                }
                System.out.println("缓存查询有的时候:"+listredis);
                System.out.println("home页面加载的数据:"+listredis.toString());
                return listredis;
            }
            }
        System.out.println("return前面的listredis:"+listredis);
        return listredis;
            }



    }


效果图:
1。第一次走的数据库:
图片说明

2.走的缓存:
图片说明

3.redis客户端:
图片说明

不明白可以留言问我

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐