标签:验证 poj 命名 文件夹 auto override key unit ring
一、安装Redis
Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases。 Redis
支持32 位和64 位。这个需要根据你系统平台的实际情况选择,这里我们下载 Redis-x64-xxx.zip压缩包到
C盘的tools目录中,解压后,将文件夹重新命名为 redis。
打开一个 cmd 窗口 使用cd命令切换目录到 C:\tools\redis 运行 redis-server.exe
redis.windows.conf 。
redis启动指令:1.cd 进入 redis目录下;2.redis-server.exe redis.windows.conf
(用redis.windows.conf配置文件启动redis)
redis登录指令: redis-cli.exe -h host -p port -a password
(redis访问默认不需要密码)Redis密码设置 设置密码:config set requirepass(密码) 密码验证:config
get requirepass 在redis.windows.conf文件中设置 requirepass 密码
退出redis shutdown exit
安装成功后,可以在windows的服务管理中对redis进行管理,就不用每次都打开命令窗口来启动redis服务了,如下图:
二、在原有项目中引入redis,先在pom.xml添加redis的依赖
<!-- 添加redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
三、在application.properties中添加redis配置信息
#redis config
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
四、新建一个config包,用来存放一些配置文件,新建RedisConfig.java
package com.hsp.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
@Configuration
@EnableCaching//开启注解
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
CacheManager cacheManager = new RedisCacheManager(redisTemplate);
return cacheManager;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}
六、在service包中建立一个RedisService.java类
package com.yjy.service;
/**
*
* Description:
*
* @author
* @since JDK1.8
* @history 2018年9月4日
*/
public interface RedisService {
/**
* Description:指定缓存失效时间
* @param key 键
* @param time 时间(秒)
* @return boolean
*/
public boolean expire(String key,long time);
/**
* Description:根据key 获取过期时间
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key);
/**
* Description:判断key是否存在
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key);
/**
* Description:普通缓存放入
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public void set(String key, Object value);
/**
* Description:普通缓存获取
* @param key 键
* @return 值
*/
public Object get(String key);
/**
* Description:删除缓存
* @param key 可以传一个值 或多个
* @return
*/
public void del(String ... key);
}
七、RedisServiceImpl.java
package com.yjy.service.impl;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.yjy.service.RedisService;
/**
*
* Description:
*
* @author
* @since JDK1.8
* @history 2018年9月4日
*/
@Service
public class RedisServiceImpl implements RedisService{
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Override
public void set(String key, Object value) {
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
vo.set(key, value);
}
@Override
public Object get(String key) {
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
return vo.get(key);
}
@Override
public boolean expire(String key, long time) {
try {
if(time>0){
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public long getExpire(String key) {
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
@Override
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public void del(String... key) {
if(key!=null&&key.length>0){
if(key.length==1){
redisTemplate.delete(key[0]);
}else{
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
}
八、StartApplication.java
package com.yjy.controller;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
*
* Description:
*
* @author
* @since JDK1.8
* @history 2018年8月30日 新建
*/
@SpringBootApplication(scanBasePackages = {"com.yjy.controller","com.yjy.service.impl","com.yjy.config"})
@EnableCaching //开启缓存
@EnableTransactionManagement // 开启事务管理
@EnableAutoConfiguration
// 配置mapper层的扫描
@MapperScan(basePackages = {"com.yjy.mapper"})
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
}
九、GoodsController.java
package com.yjy.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yjy.pojo.Goods;
import com.yjy.service.GoodsService;
import com.yjy.service.RedisService;
/**
*
* Description:
*
* @author
* @since JDK1.8
* @history 2018年8月30日 新建
*/
@Controller
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Autowired
private RedisService redisService;
private Goods goods;
@RequestMapping("/selectBy.do")
@ResponseBody
public Goods selectBy(String name,int num) {
//判断是否存在缓存
if(redisService.hasKey(list)){
//存在缓存 通过换窜的key获取缓存的数据
goods=redisService.get(list);
}else{
//不存在缓存去数据库查询
goods = goodsService.selectBy(name,num);
redisService.set(“list”, goods);
}
return goods;
}
// 通过key获取value
@RequestMapping("/getgoodsfromredis.do")
@ResponseBody
public Goods getRedis(@RequestParam String key) {
return (Goods) redisService.get(key);
}
// 根据key获取缓存过期时间
@RequestMapping("/getTime.do")
@ResponseBody
public long getExpire(@RequestParam String key) {
return redisService.getExpire(key);
}
// 根据key删除缓存
@RequestMapping("/delRediskey.do")
@ResponseBody
public void del(@RequestParam String ... key) {
redisService.del(key);
}
}
标签:验证 poj 命名 文件夹 auto override key unit ring
原文地址:https://www.cnblogs.com/wang66a/p/12069310.html