您的位置:首页 > 数据库 > Redis

Redis

2020-07-31 18:20 531 查看

1.什么是Redis?

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

优势:

1.性能极高 – Redis能支持超过 10W次每秒的读写频率。

2.丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。

3.原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作合并后的原子性执行(简单的事务)。

4.丰富的特性 – Redis还支持 publish/subscribe(发布/订阅), 通知, key 过期等等特性。

2.理解Redis的键值对结构

2.1依赖于KEY--KEY的类型

2.2VALUE的数据结构类型

 在redis中,value的可选类型很多,String,list,set,orderset,hash

Map<String,List>   Map<String,Set>    Map<String,String>   Map<String,Map<>>

3.Redis的安装

安装完后的安装目录中有几个常用的文件:

  1. redis.windows.conf文件--当我我们安装成功以后如果启动redis的时候有错误,那么我们需要打开这个文件修改maxheap配置属性的数据值和maxmemory配置属性的数据值。
  2. redis-server.exe文件--启动redis服务【默认安装成功以后机会自动启动】
  3. redis-cli.exe文件--redis客户端窗口启动。

4.Redis中的命令

SET  KEY  VALUE:把VALUE保存到redis中KEY对应的值;

GET KEY:取出redis中KEY对应的值;

5.JAVA中对Redis进行操作

5.1.Spring Data Redis框架

[code](1)构建Maven工程  SpringDataRedisDemo
(2)引入Spring相关依赖、引入JUnit依赖 Jedis和SpringDataRedis依赖
<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>com.click369.test</groupId>
<artifactId>SpringDataRedisDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
<scope>test</scope>
</dependency>

<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 编译环境的版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
(4)在src/main/resources下创建properties文件夹,建立redis-config.properties
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

(5)在src/main/resources下创建spring文件夹 ,创建applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载redis-config.properties -->
<context:property-placeholder location="classpath:redis-config.properties" />
<!--  redis链接池相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- JedisConnectionFactory对象 -->
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.pass}"></property>
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!-- redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory"></property>
</bean>
</beans>

 

5.2.Spring Data Redis操作Redis中的String

[code]package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestRedis {
@Autowired
private  RedisTemplate  redisTemplate;
/**
* 操作字符串数据
*/
@Test
public  void testRedisString(){
//保存String数据到Redis中
//redisTemplate.boundValueOps("testName").set("网星软件");
//取出指定的键对应的数据值
String value=(String)redisTemplate.boundValueOps("testName").get();
System.out.println("testName=="+value);
//删除String数据
//redisTemplate.delete("testName");
}
}

5.3.Spring Data Redis操作Redis中的Set

[code]/**
* Set类型操作
*/
@Test
public  void testRedisSet(){
//添加数据进入set
//add(Object... values)---可变参数
redisTemplate.boundSetOps("myset").add("曹操");
redisTemplate.boundSetOps("myset").add("刘备");
redisTemplate.boundSetOps("myset").add("孙权");
//提取set中的数据
Set myset=redisTemplate.boundSetOps("myset").members();
//遍历set集合
for(Object obj:myset){
String data=(String)obj;
System.out.println(data);
}
System.out.println("-------------------------------");
//添加数据进入set
Object  names[]={"关羽","张飞","赵云","马超","黄忠"};
redisTemplate.boundSetOps("myname").add(names);
//{"myname":["关羽","张飞","赵云","马超","黄忠"]}
//删除set中的值
//remove(value)
//redisTemplate.boundSetOps("myname").remove("赵云");
//redisTemplate.boundSetOps("myname").remove(new Object[]{"马超","黄忠"});
//删除所有
redisTemplate.delete("myname");
//提取set中的数据
Set myname=redisTemplate.boundSetOps("myname").members();
//遍历set集合
for(Object obj:myname){
String data=(String)obj;
System.out.println(data);
}
}

5.4.Spring Data Redis操作Redis中的List

[code]/**
* List类型操作
*/
@Test
public  void testRedisList(){
//右压栈---后添加的对象排在后边【从右向左压数据】
/*
redisTemplate.boundListOps("mylist1").rightPush("许褚");
redisTemplate.boundListOps("mylist1").rightPush("典韦");
redisTemplate.boundListOps("mylist1").rightPush("徐晃");
redisTemplate.boundListOps("mylist1").rightPush("张辽");
redisTemplate.boundListOps("mylist1").rightPush("夏侯惇");
//返回存储在 key 的列表里指定范围内的元素
//range(start, end);
//start 和 end 偏移量都是基于0的下标,即list的第一个元素下标是0(list的表头),第二个元素下标是1,以此类推。
//偏移量也可以是负数,表示偏移量是从list尾部开始计数。 例如, -1 表示列表的最后一个元素,-2 是倒数第二个,以此类推。
List mylist1=redisTemplate.boundListOps("mylist1").range(0, -1);
for(Object  obj:mylist1){
String data=(String)obj;
System.out.println(data);
}
*/
//左压栈---后添加的对象排在前边[从左向右压数据]
redisTemplate.boundListOps("mylist2").leftPush("许褚");
redisTemplate.boundListOps("mylist2").leftPush("典韦");
redisTemplate.boundListOps("mylist2").leftPush("徐晃");
redisTemplate.boundListOps("mylist2").leftPush("张辽");
redisTemplate.boundListOps("mylist2").leftPush("夏侯惇");
//删除所有
//redisTemplate.delete("mylist2");
//根据索引查询元素
//String name=(String)redisTemplate.boundListOps("mylist2").index(3);
//System.out.println("name==="+name);
//从存于 key 的列表里移除前 count 次出现的值为 value 的元素。
//这个 count 参数通过下面几种方式影响这个操作:
//count > 0: 从头往尾移除值为 value 的元素。
//count < 0: 从尾往头移除值为 value 的元素。
//count = 0: 移除所有值为 value 的元素。
Long l=redisTemplate.boundListOps("mylist2").remove(-2, "夏侯惇");
System.out.println("l==="+l);
List mylist2=redisTemplate.boundListOps("mylist2").range(0, -1);
for(Object  obj:mylist2){
String data=(String)obj;
System.out.println(data);
}
}

5.5.Spring Data Redis操作Redis中的Map

[code]实际上就是在Redis这个巨大的Map中在保存一个小map.
最终的形式Map<String,Map<String,Object>>.
/**
* Hash类型操作
*/
@Test
public  void testRedisHash(){
//存入数据
/*
redisTemplate.boundHashOps("myhash1").put("a","唐僧");
redisTemplate.boundHashOps("myhash1").put("b","孙大圣");
redisTemplate.boundHashOps("myhash1").put("c","猪八戒");
redisTemplate.boundHashOps("myhash1").put("d","沙和尚");
redisTemplate.boundHashOps("myhash1").put("e","白龙马");
*/
/*
//提取所有的KEY
Set  keyset=redisTemplate.boundHashOps("myhash1").keys();
for(Object k:keyset){
System.out.println("key=="+k);
}
//提取所有的VALUE
List valuelist=redisTemplate.boundHashOps("myhash1").values();
for(Object v:valuelist){
System.out.println("value=="+v);
}
System.out.println("---------------------");
*/
//存入数据
/*
Map<String,Object> m=new HashMap<String,Object>();
m.put("1", "java");
m.put("2", "java SE");
m.put("3", "java EE");
m.put("4", "java ME");
redisTemplate.boundHashOps("myhash2").putAll(m);
*/
//提取所有的KEY
Set  keyset2=redisTemplate.boundHashOps("myhash2").keys();
for(Object k:keyset2){
System.out.println("key=="+k);
}
//提取所有的VALUE
List valuelist2=redisTemplate.boundHashOps("myhash2").values();
for(Object v:valuelist2){
System.out.println("value=="+v);
}
//根据KEY提取值
String val=(String)redisTemplate.boundHashOps("myhash2").get("2");
System.out.println("val=="+val);
//根据KEY移除值
Long l=redisTemplate.boundHashOps("myhash2").delete("3");
System.out.println("l=="+l);
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: