- 什么是redis:
- reddis 是一个nosql的(not only sql) - 关系形数据库:以二维表的形式存储数据 - 是一家意大利的创业公司 出的,后来被vmware收购
redis应用领域:
- 分布式缓存 - 分布式Session - 保存留言或者论坛的留言的回复等。总之是用在数据量大,
- redis的优点:读速度在30万次写速度为10万次作用
- 缺点:对于数据持久化支持不太好
redis的数据类型:
String(字符串)String是redis的最基本的类型,你可以理解成Memcached 一样的类型,一个key对应一个value,我们要明确的是String类型并非是我们所理解的java的字符串类型,String类型是二进制安全的。所以redis的String类型可以存储任何类型的数据,包括图片,以及java中的对象。string最大为512M
使用方法:
1
2
3
4redis 127.0.0.1:6379> SET name "runoob"
OK
redis 127.0.0.1:6379> GET name
"runoob"
Hash(哈希)
Redis hash 是一个键值对集合 (key=value)Redis hash 是一个String类型的field和value的映射表,hash特别适用于存储对象。
1
2
3
4
5
6redis> HMSET myhash field1 "Hello" field2 "World"
"OK"
redis> HGET myhash field1
"Hello"
redis> HGET myhash field2
"World"
- List列表是简单的字符串列表,按照插入顺序排序,你可以添加列表头部和列表尾部
1
2
3
4
5
6
7
8
9
10
11
redis 127.0.0.1:6379> lpush runoob redis
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>
- Redis的Set是string类型的无序集合。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
- sadd命令:添加一个String 元素到key对于的set集合中,成功返回1,如果元素已经在集合中返回0,如果key对应的set不存在则返回错误。
1
sadd key member
- 实例
1
2
3
4
5
6
7
8
9
10
11
12
13
redis 127.0.0.1:6379> sadd runoob redis
(integer) 1
redis 127.0.0.1:6379> sadd runoob mongodb
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd runoob rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers runoob
1) "redis"
2) "rabitmq"
3) "mongodb"
- zset(sorted set : 有序集合)
- Redis zset 和set一样也是String类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合从大到小进行排列顺序。
- zset的 成员是唯一的,但是分数却可以重复 。
- zadd命令:添加元素到集合,元素在集合中存在则更新对应的score
1
zadd key score member
- 实例
1
2
3
4
5
6
7
8
9
10
11
12
redis 127.0.0.1:6379> zadd runoob 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd runoob 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> > ZRANGEBYSCORE runoob 0 1000
1) "mongodb"
2) "rabitmq"
3) "redis"
在java中配置redis,主要用的是jredis作为客户端:我们想提高一下难度所以采用Spring容器来进行获取jredis:
spring-application.xml文件的
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<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--读取redis的配置文件 -->
<context:property-placeholder location="classpath:redis.properties" />
<!--将配置文件值赋予参数中 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!--配置来接数据工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="poolConfig" ref="poolConfig"></property>
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="timeout" value="15000"></property>
<property name="usePool" value="true"></property>
</bean>
<!--配置序列化 模板对象需要这个对象作为参数,因为String的是byte类型 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!--通过redis模板通过工厂获取模板对象 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="redisService" class="com.hjy.testredis.TestRedis"/>
</beans>
- pom.xml文件:主要依赖一些spring的一些jar包
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestRedis</groupId>
<artifactId>com.hjy.testredis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- redis的配置文件
1
2
3
4
5
6
7
# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
- java文件的配置
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
public static void main(String[] args) {
// 读取Spring的配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:application-spring.xml");
TestRedis bean = (TestRedis) ac.getBean("redisService");
TestRedis redisService = (TestRedis) ac.getBean("redisService");
/**
* 使用的redis的模板进行的连接
*/
StringRedisSerializer stringRedisSerializer = (StringRedisSerializer) ac.getBean("stringRedisSerializer");
RedisTemplate redisWriteTemplate = (RedisTemplate) ac.getBean("redisTemplate");
redisWriteTemplate.setKeySerializer(stringRedisSerializer);
redisWriteTemplate.setValueSerializer(stringRedisSerializer);
ValueOperations x = redisWriteTemplate.opsForValue();
x.set("hjy", "you wii make it");
System.out.println(x.get("hjy"));
// redisWriteTemplate.setHashKeySerializer(stringRedisSerializer);
// redisWriteTemplate.setHashValueSerializer(stringRedisSerializer);
HashOperations opsForHash = redisWriteTemplate.opsForHash();
opsForHash.putIfAbsent("hjyy", "hello", "world");
Object object = opsForHash.get("hjyy", "hello");
System.out.println(object);
/**
* 使用的jedis的连接的客户端
*/
// System.out.println(TestRedis.jedisConnectionFactory);
// JedisConnectionFactory bean2 = (JedisConnectionFactory)
// ac.getBean("connectionFactory");
// Jedis jedis = bean2.getShardInfo().createResource();
// jedis.set("hjy", "you will be successful");
// System.out.println(jedis.get("hjy"));
}
- 我一直在纠结一个问题,为什么使用主函数的时候,我们的依赖注入就不起作用了,而只能通过application的getbean 方法来获得的,主要是因为 他的执行的顺序不同,我们在执行主函数,在依赖注入的时候,我们的Spring 在main函数里没有实例化所以bean对象也没有注入到spring容器里,所以我们在获取bean的时候获取的是空指针。