• Redis 应用场景及操作


    Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。

    Redis 与其他 key - value 缓存产品有以下三个特点:

    • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
    • Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
    • Redis支持数据的备份,即master-slave模式的数据备份。

    Redis优势:

    • 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s
    • 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作
    • 原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性。
    • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性

    Redis中的数据结构:

    • String(字符串):特点就是key - value类型
    • Hash(哈希结构):特点是key - (field - value), (field2 - value2), (field3- value3)......
    • List(链表):特点是 key - value1,value2,value3,value4....
    • Set(集合):特点是key - value1,value2,value3.....(集合的value是无顺序的)
    • hSet(有序集合):特点key - (value:score),(value:score),(value:score)... (有序集合是根据value的评分排序)
    • 基数

    Redis应用场景

    1)缓存服务器

     2)高并发的读写

     3)数据共享

     

     4)分布式锁

     

    5)自增长编号的生成

     java操作redis

    # 导入依赖
    <dependency>
         <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
      <version>3.1.0</version>
    </dependency>
    public static void main(String[] args) {
            //连接redis
            Jedis jedis = new Jedis("192.168.189.130",6379);
            jedis.auth("root"); // 密码
            //操作redis
            jedis.set("name","xiaoming");
            jedis.set("age","19");
            Object name = jedis.get("name");
            Object age = jedis.get("age");
            System.out.println(name+" "+age); // xiaoming 19
            //关闭连接
            jedis.close();
        }

    Spring操作redis

    1.导入依赖

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.1.0</version>
    </dependency>

    2.配置applicationContext-redis.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- 配置redis连接池对象 -->
        <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!-- 最大空闲数 -->
            <property name="maxIdle" value="50"/>
            <!-- 最大连接数 -->
            <property name="maxTotal" value="100"/>
            <!-- 最大等待时间 -->
            <property name="maxWaitMillis" value="20000"/>
        </bean>
    
        <!-- 配置redis连接工厂 -->
        <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <!-- 配置连接池 -->
            <property name="poolConfig" ref="poolConfig"/>
            <!-- 配置主机 -->
            <property name="hostName" value="192.168.189.130"/>
            <!-- 配置端口 -->
            <property name="port" value="6379"/>
            <!-- 配置密码 -->
            <property name="password" value="root"/>
        </bean>
    
        <!-- 配置redis模板对象 -->
        <bean class="org.springframework.data.redis.core.RedisTemplate">
            <!-- 配置连接工厂 -->
            <property name="connectionFactory" ref="connectionFactory"/>
        </bean>
    </beans>
    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-redis.xml")
    public class Main {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void test(){
            redisTemplate.opsForValue().set("name","liming");
            redisTemplate.opsForValue().set("age",21);
            Object name = redisTemplate.opsForValue().get("name");
            Object age = redisTemplate.opsForValue().get("age");
            System.out.println(name+" "+age); // liming 21
        }
    }

    SpringBoot操作redis

    1.导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>

    2.配置application.yml

    spring:
      redis:
        host: 192.168.189.130
        password: root
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootRedisApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void contextLoads() {
            redisTemplate.opsForValue().set("name","xiaoli");
            redisTemplate.opsForValue().set("age",23);
            Object name = redisTemplate.opsForValue().get("name");
            Object age = redisTemplate.opsForValue().get("age");
            System.out.println(name+" "+age); // xiaoli 23
        }
    }

    Redis分布锁 https://www.cnblogs.com/wakey/p/10725786.html

  • 相关阅读:
    CF1168B Good Triple 性质分析
    bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
    BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机+栈
    BZOJ 1691 [Usaco2007 Dec]挑剔的美食家 multiset+排序+贪心
    BZOJ 1725: [Usaco2006 Nov]Corn Fields牧场的安排 状压动归
    BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路 Dijkstra
    BZOJ 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 幼儿园测试题
    BZOJ 5508: [Tjoi2019]甲苯先生的字符串 矩阵乘法_思维
    BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
    描述符get/set/delete,init/new/call,元类
  • 原文地址:https://www.cnblogs.com/wakey/p/12703448.html
Copyright © 2020-2023  润新知