首页>代码>springboot整合jedis项目实例>/springboot-jedis/src/main/java/com/trq/springbootjedis/Service/Impl/UserServiceImpl.java
package com.trq.springbootjedis.Service.Impl;

import com.trq.springbootjedis.Service.UserService;
import com.trq.springbootjedis.Util.JedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public String getString(String key) {
        // 获取Jedis对象
        Jedis jedis = jedisUtil.getJedis();

        String val = null;

        // 判断key是否存在于Redis
        if(jedis.exists(key)) {

            val = jedis.get(key);
           logger.info(key + "是在Redis中查询到的,值为" + val);
        } else {

            val = "test";   // 模拟从MySQL中查询到的数据
            logger.info(key + "是在MySQL中查询到的,值为" + val);
            jedis.set(key, val);
            logger.info(key + "存入Redis中,值为" + val);
        }

        // 关闭jedis
        jedisUtil.close(jedis);

        return val;
    }
}
最近下载更多
wxd1997  LV13 4月16日
是一个鸽子啊  LV17 2023年6月14日
1145304128  LV12 2021年12月1日
shiopaaa  LV13 2021年11月23日
尹恒yingying  LV18 2021年10月21日
安东尼online  LV11 2021年7月15日
123456nty  LV37 2021年6月6日
wcy071213  LV27 2020年8月22日
skipple3  LV39 2020年5月31日
bjgaocl  LV13 2020年3月21日
最近浏览更多
wxd1997  LV13 4月16日
zhujunnan  LV12 3月19日
guoshuai09  LV4 3月13日
唐唐丶  LV33 2023年11月14日
wnnmmb  LV2 2023年10月19日
是一个鸽子啊  LV17 2023年6月14日
guviva  LV6 2023年5月29日
1025490081  LV2 2022年11月9日
包呼和  LV10 2022年10月19日
罗清晨  LV11 2022年10月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友