编程世界数据库我爱编程

【分布式架构之旅】Redis入门

2017-12-04  本文已影响1040人  cmazxiaoma

前言

昨天和室友去包夜,玩了一晚上的LOL,跪了一整夜,但是很开心。从S1末开始玩LOL的我,到现在还是青铜,真是菜的抠脚。最近负能力满满的,唯有睡觉和学习才可解忧愁。今天也看了慕课网上面的《Redis入门》,来记一下学习笔记。(写这篇文章开头的时候应该是一个星期之前)

DNA.png

NoSQL概述

image.png

Redis的概述


Redis在Linux上的使用

可以看我这篇文章【Linux学习】 Redis常用的一些指令


Jedis的入门

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    public void methodOne() {
        Jedis jedis = new Jedis("100.64.84.47", 6379);
        jedis.set("name", "cmazxiaoma");
        String value = jedis.get("name");
        System.out.println(value);
        jedis.close();
    }
  public void methodTwo() {
        //获得连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大连接数
        config.setMaxTotal(30);
        //设置最大空闲连接数
        config.setMaxIdle(10);
        //获得连接池
        JedisPool jedisPool = new JedisPool(config, "100.64.84.47", 6379);

        Jedis jedis = null;

        try {
            jedis = jedisPool.getResource();
            String value = jedis.get("name");
            System.out.println(value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }

            jedisPool.close();
        }
    }
}
public class JedisDemo1Test {
    private JedisDemo1 demo;

    @Before
    public void setUp() {
        demo = new JedisDemo1();
    }

    @Test
    public void methodOne() throws Exception {
        demo.methodOne();
    }

    @Test
    public void methodTwo() throws Exception {
        demo.methodTwo();
    }

}

Redis的数据结构

String

如果属性不存在的话,那么integer类型默认为0

image.png

如果name属性的值不能转换成integer类型,那么会抛出ERR is not an integer or out of range异常。

image.png

decr指令也是一样的。

image.png

append指令可以拼接字符串。

image.png

如果append key cmazxiaoma。这个key不存在的话,首先会创建这个key,然后存入cmazxiaoma内容,接着输出cmazxiaoma

image.png

Hash

hdel myhash key key删除多个的key

image.png

del myhash删除myhash中所有的key

image.png

List

image.png

Set

List类型不同的是,Set集合中不允许出现重复的元素。


SortedSet

SortedSet中的成员在集合中的位置是有序的。


Redis中的通用命令


Redis的事务


Redis的持久化

Redis的性能体现在它把数据都保存在内存当中。我们把内存中的数据同步到硬盘当中的操作称之为持久化。

RDB.png AOF.png

尾言

这篇文章最后在网吧完成的,勿以善小而不为。

上一篇 下一篇

猜你喜欢

热点阅读