一、下载Redis
- 下载Redis并解压缩https://redis.io/download
- 在解压文件夹开启命令行。
G:\Software\redis-64.3.0.503>redis-server.exe redis.windows.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.503 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 3836
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[3836] 29 Nov 16:39:12.287 # Server started, Redis version 3.0.503
[3836] 29 Nov 16:39:12.301 * DB loaded from disk: 0.006 seconds
[3836] 29 Nov 16:39:12.303 * The server is now ready to accept connections on port 6379
二、测试简单读取写入
参数:
-h 目标主机IP地址
-p 目标主机Redis服务器监听端口
G:\Software\redis-64.3.0.503>redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> set me wyf
OK
127.0.0.1:6379> get me
"wyf"
三、maven web程序中使用redis
pom.xml添加依赖包
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
- 简单的使用,创建Jedis对象传入主机IP地址,端口。并通过get方法获取对象。结束后调用close方法关闭
- 不同的客户端都可以进行访问,因此可以实现内存对象共享功能
@RequestMapping("/g-greet")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
String host = "127.0.0.1";
int port = 6379;
Jedis jedis = new Jedis(host,port);
String strInRedis = jedis.get(name);
jedis.close();
model.addAttribute("name", strInRedis);
return "greeting";
}