首页>代码>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; } }