redis笔记

概述

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

关键字: 数据库, 缓存, key value, 高效,可以集群部署, noSQL

安装部署

docker run -dit --name redis -p6379:6379 redis
firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --reload

测试连接

Java中使用jedis操作redis数据库

pom加入依赖:

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

编写测试连接类RedisStandalone.class:

import redis.clients.jedis.Jedis;

/**
 * 单机版redis
 */
public class RedisStandalone {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.126.133", 6379);
        jedis.set("user1", "linjb");
        jedis.setDataSource();
        String user = jedis.get("user1");
        System.out.println(user);
    }
}

redis可视化软件

软件:Redis Desktop Manager 官网:https://redisdesktop.com/

Java Api操作

https://github.com/josiahcarlson/redis-in-action

3. redis命令

3.1 字符串 incr #自增 decr #自减 incrby #自增(指定增加数) decrby #自减(同上) 示例: incr nae # nae增加1 incrby nae 10 # nae增加10

append # 追加 getrange # 截取指定范围 setrange # 替换指定范围