20221101 Redis总结

Redis

官网:https://www.redis.net.cn/tutorial/3503.html
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统。

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

它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

下载

github官方下载:https://github.com/dmajkic/redis/downloads

加载服务器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Java原生连接redis

添加依赖

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.2.3</version>
</dependency>

使用Jedis对象

public class RedisJava {
   public static void main(String[] args) {
      //连接本地的 Redis 服务
      //Jedis构造参数有多种,具体可看源码
      Jedis jedis = new Jedis("localhost");
      //连接后可以使用String ,List等对象
      System.out.println("Connection to server sucessfully");
      //查看服务是否运行
      System.out.println("Server is running: "+jedis.ping());
 }
}

SSM连接redis

添加依赖

如果存在版本兼容问题可以更换jedis或者spring-data-redis版本。

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.7.0</version>
</dependency>

添加xml配置文件

redis-context.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:aop="http://www.springframework.org/schema/aop" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xsi:schemaLocation=" 
                     http://www.springframework.org/schema/beans 
                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
                     http://www.springframework.org/schema/context 
                     http://www.springframework.org/schema/context/spring-context-4.0.xsd "> 
 
    

    <!-- redis数据源 --> 
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <!-- 最大空闲数 --> 
        <property name="maxIdle" value="10" /> 
        <!-- 最大空连接数 --> 
        <property name="maxTotal" value="100" /> 
        <!-- 最大等待时间 --> 
        <property name="maxWaitMillis" value="2000" /> 
        <!-- 返回连接时,检测连接是否成功 --> 
        <property name="testOnBorrow" value="true" /> 
    </bean> 
 
    <!-- Spring-redis连接池管理工厂 --> 
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
        <!-- IP地址 --> 
        <property name="hostName" value="127.0.0.1" /> 
        <!-- 端口号 --> 
        <property name="port" value="6379" /> 
        <!--<property name="password" value="" />--> 
        <!-- 超时时间 默认2000--> 
        <property name="timeout" value="2000" /> 
        <!-- 连接池配置引用 --> 
        <property name="poolConfig" ref="poolConfig" /> 
        <!-- usePool:是否使用连接池 --> 
        <property name="usePool" value="true"/> 
    </bean> 
 
    <!-- redis template definition --> 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 
        <property name="connectionFactory" ref="jedisConnectionFactory" /> 
        <property name="keySerializer"> 
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 
        </property> 
        <property name="valueSerializer"> 
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 
        </property> 
        <property name="hashKeySerializer"> 
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 
        </property> 
        <property name="hashValueSerializer"> 
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 
        </property> 
        <!--开启事务  --> 
        <property name="enableTransactionSupport" value="true"></property> 
    </bean> 
 
    
</beans>

applicationContext.xml(Spring配置文件) 加入

<import resource="classpath:redis-context.xml"></import>

使用RedisTemplate对象

@Autowired
private RedisTemplate redisTemplate;

接下来就可以使用 redisTemplate.opsForValue() 获取redis的String数据结构
redisTemplate.opsForList()
redisTemplate.opsForHash()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值