Fork me on GitHub

如何安装redis和简单使用

前言

在github上有个小项目,spring+springMVC+mybatis搭建的秒杀系统,简单的模拟秒杀、减库存、避免重复秒杀等问题。同时使用redis作为缓存、Protostuff作为序列化工具。传送门

现在就来简单记录下安装redis和使用的方法。

下载

windows平台下载

Windows平台下官方并不支持redis下载的,只是在github上微软自己针对Windows平台开发了。https://github.com/MicrosoftArchive/redis/releases/

Windows下载截图

可能上面github下载地址速度很慢。

我做了一个备份,方便大家下载

  • 解压版

解压版下载

  • 解压版

安装版下载

Linux平台下载

linux平台下载直接使用wget命令下载。

1
2
3
4
5
6
7
8
9
10
11
[root@iZj1fkye8uu7o0Z ~]# wget http://download.redis.io/releases/redis-4.0.6.tar.gz
--2018-01-18 13:27:29-- http://download.redis.io/releases/redis-4.0.6.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.6.tar.gz.1’

100%[====================================================================================================================>] 1,723,533 1.10MB/s in 1.5s

2018-01-18 13:27:32 (1.10 MB/s) - ‘redis-4.0.6.tar.gz.1’ saved [1723533/1723533]

查看自己下载的内容

1
2
3
[root@iZj1fkye8uu7o0Z ~]# ll redis-4.0.6.tar.gz
-rw-r--r-- 1 root root 1723533 Dec 5 01:02 redis-4.0.6.tar.gz
[root@iZj1fkye8uu7o0Z ~]#

安装

Windows平台安装

  • 安装版

Windows下安装起始很简单的。双击运行,选择安装路径,默认端口号。finish完事。

安装

端口号

安装

从Windows的【服务】当中可以查看redis服务在运行。

服务

  • 解压版

解压版只需要使用CMD命令,进入解压目录,然后将redis服务添加进入系统服务就行了。

Linux平台安装

-解压

解压之后可以看到redis目录当中存在这些内容。

1
2
3
4
5
[root@iZj1fkye8uu7o0Z ~]# tar xzf redis-4.0.6.tar.gz
[root@iZj1fkye8uu7o0Z ~]# ls redis-4.0.6
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
[root@iZj1fkye8uu7o0Z ~]#
  • 编译

首先进入redis目录,然后编译

1
2
[root@iZj1fkye8uu7o0Z ~]# cd redis-4.0.6
[root@iZj1fkye8uu7o0Z redis-4.0.6]# make
  • copy文件

编译完成后将src目录下这几个可执行文件copy到/usr/redis目录下

1
2
3
4
5
6
mkdir /usr/redis
cp redis-server /usr/redis
cp redis-benchmark /usr/redis
cp redis-cli /usr/redis
cp redis.conf /usr/redis/
cd /usr/redis

结果如下:

1
2
3
4
5
[root@iZj1fkye8uu7o0Z redis]# ls
redis-benchmark redis-cli redis.conf redis-server
[root@iZj1fkye8uu7o0Z redis]# pwd
/usr/redis
[root@iZj1fkye8uu7o0Z redis]#
  • 启动redis
1
2
3
[root@iZj1fkye8uu7o0Z redis]# ./redis-server 
2358:C 18 Jan 13:59:50.867 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2358:C 18 Jan 13:59:50.867 # Redis version=4.0.6, bits=64, commit=00000000, modified=0, pid=2358, just started

切记这个窗口不要关闭,然后再开一个窗口执行客户端命令

1
2
3
4
5
6
7
[root@iZj1fkye8uu7o0Z ~]# cd /usr/redis/
[root@iZj1fkye8uu7o0Z redis]# ./redis-cli
127.0.0.1:6379> set key 'hello world'
OK
127.0.0.1:6379> get key
"hello world"
127.0.0.1:6379>

set命令保存一个键值对为 key——hello world

简单使用

下载依赖

在java项目当中使用redis,我们首先要下载依赖Jedis

  • 下载Jedis依赖
1
2
3
4
5
6
<!--Redis客户端:Jedis连接-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
  • 使用Protostuff序列化

在这里我是用的是Protostuff依赖。同样你也可以使用继承Serializable来实现序列化,但是时间,空间方面都没有Protostuff好。具体的可以看这里

1
2
3
4
5
6
7
8
9
10
11
<!--protostuff序列化依赖-->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>

看看代码

  • 首先使用redis

我这里是简单介绍下如何使用redis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @author chencong , Created in 2018/1/17 21:17
*/
public class RedisDao {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final JedisPool jedisPool;

// 这是是使用Protostuff创建一个目标实体的模板
private RuntimeSchema<Seckill> schema = RuntimeSchema.createFrom(Seckill.class);


// 构造器指定redis的ip和端口,这里是配合springMVC 单元测试使用,注入ip和port在spring-dao.xml当中
public RedisDao(String ip, int port) {
jedisPool = new JedisPool(ip, port);
}

/**
* redis操作逻辑,从redis当中获取到seckill
*
* @param seckillId
* @return
*/
public Seckill getSeckill(Long seckillId) {
//redis操作逻辑
try {
Jedis jedis = jedisPool.getResource();
try {
String key = "seckill:" + seckillId;
// 并没有实现内部序列化
// get=>byte[]=>反序列化 =>Object(seckill)
// 采用自定义序列化方式protostuff
// protostuff : pojo
byte[] bytes = jedis.get(key.getBytes());
//缓存重新获得到
if (bytes != null) {
Seckill seckill = schema.newMessage();
ProtostuffIOUtil.mergeFrom(bytes, seckill, schema);
//seckill被反序列化
return seckill;
}
} finally {
jedis.close();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;

}

public String putSeckill(Seckill seckill) {
// set Object(Seckill) => 序列化 =》byte[]
try{
Jedis jedis = jedisPool.getResource();
try{
String key = "seckill:"+seckill.getSeckillId();
byte[] bytes = ProtostuffIOUtil.toByteArray(seckill,schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//缓存超时
int timeout = 60 * 60;
String result = jedis.setex(key.getBytes(),timeout,bytes);
return result;
}finally {
jedis.close();
}
}catch (Exception e){
logger.error(e.getMessage(),e);
}
return null;
}
}

再来看看关于这个redisDao的redis的注入参数部分的代码。

1
2
3
4
5
<!--RedisDao 使用构造方法注入,并且指定参数值-->
<bean id="redisDao" class="cc.ccoder.dao.cache.RedisDao">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="6379"/>
</bean>
  • 单元测试

看看单元测试。首先从redis缓存当中查找,存在直接返回,不存在从数据库当中查找,查找到了数据并且放入redis缓存当中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

/**
* @author chencong , Created in 2018/1/17 21:42
*/

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring/spring-dao.xml", "classpath:spring/spring-dao.xml"})
public class RedisDaoTest {

private long id = 1001;

@Autowired
private SeckillDao seckillDao;

@Autowired
private RedisDao redisDao;


@Test
public void testSeckill() throws Exception {
// get and set
Seckill seckill = redisDao.getSeckill(id);
if (seckill == null) {
seckill = seckillDao.queryById(id);
if (seckill != null) {
String result = redisDao.putSeckill(seckill);
System.out.println(result);
seckill = redisDao.getSeckill(id);
System.out.println(seckill);
}
}
}
}

好啦,所有讲解使用部分就这样了,具体的代码部分可以查看github上面项目。

https://github.com/chencong-plan/seckill

联系

聪聪的独立博客 ,一个喜欢技术,喜欢钻研的95后。如果你看到这篇文章,千里之外,我在等你联系。

坚持原创技术分享,您的支持将鼓励我继续创作!