redis 入门(一)——Linux环境安装测试以及基本命令演示

时间:2022-07-25
本文章向大家介绍redis 入门(一)——Linux环境安装测试以及基本命令演示,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

redis概述

redis是一个开源的,先进的 key-value 存储可用于构建高性能的存储解决方案。它支持数据结构有字符串,哈希,列表,集合,带有范围查询的排序集,位图,超文本和具有半径查询的地理空间索引。 NoSQL,Not Only [SQL],泛指非关系型的数据库。所以redis是一种nosql。敲黑板画重点:redis是一种nosql.

redis的优点:

异常快速 支持丰富的数据类型 操作都是原子的

下载安装

$ wget http://download.redis.io/releases/redis-3.2.6.tar.gz 
/root/redis tar xzf redis−3.2.6.tar.gz
/root/redis/redis-3.2.6   make
启动服务器:

 ```
    $ src/redis-server
 ```

 启动客户端

 ```
 $ src/redis-cli
 ```

启动:

redis-server redis-cli

11645:C 23 Apr 14:48:49.784 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 11645
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

11645:M 23 Apr 14:48:49.788 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
11645:M 23 Apr 14:48:49.788 # Server started, Redis version 3.2.6
11645:M 23 Apr 14:48:49.788 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
11645:M 23 Apr 14:48:49.788 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
11645:M 23 Apr 14:48:49.788 * DB loaded from disk: 0.000 seconds
11645:M 23 Apr 14:48:49.788 * The server is now ready to accept connections on port 6379

redis 支持的数据类型

启动客户端 ,存储字符串到redis.

127.0.0.1:6379> set name wonder
OK

取字符串:

127.0.0.1:6379> get name
"wonder"

哈希值

127.0.0.1:6379> hmset king username wonder password root age 22
OK
127.0.0.1:6379> hgetall king
1) "username"
2) "wonder"
3) "password"
4) "root"
5) "age"
6) "22"

Lists - 列表

127.0.0.1:6379> lpush pricess yiyue
(integer) 1
127.0.0.1:6379> lpush pricess chen
(integer) 2
127.0.0.1:6379> lpush privess yinli
(integer) 1
127.0.0.1:6379> lpush pricess yinli
(integer) 3
127.0.0.1:6379> lrange pricess 0 10
1) "yinli"
2) "chen"
3) "yiyue"
127.0.0.1:6379> 

Redis 有序集合

Redis有序集合类似Redis集合存储在设定值唯一性。不同的是,一个有序集合的每个成员带有分数,用于以便采取有序set命令,从最小的到最大的分数有关。

127.0.0.1:6379> zadd kindom 1 redis
(integer) 1
127.0.0.1:6379> zadd kindom 2 mongodb
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 1
127.0.0.1:6379> zadd kindom 3 mysql
(integer) 0
127.0.0.1:6379> zadd kindom 4 mysql
(integer) 0
127.0.0.1:6379> zrange kindom 0 10 withscores
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

redis 发布订阅

开启客户端作为接受者

127.0.0.1:6379> subscribe myking messages
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2

开启另一个客户端作为发送者:

127.0.0.1:6379> publish myking redis
(integer) 1
127.0.0.1:6379> publish myking "hello redis,you are a great caching technique"
(integer) 1

注意订阅端的变化:

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myking"
3) (integer) 1
1) "subscribe"
2) "messages"
3) (integer) 2
1) "message"
2) "myking"
3) "redis"
1) "message"
2) "myking"
3) "hello redis,you are a great caching technique"

其他的一些操作

1.获取所以的key KEYS *

127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "privess"
4) "king"
5) "pricess"

2.判断key是否存在

EXISTS key 存在返回 1,不存在返回 0

127.0.0.1:6379> exists privess
(integer) 1
127.0.0.1:6379> exists privesssssss
(integer) 0

3.删除key

DEL key [key …]

127.0.0.1:6379> del privess
(integer) 1
127.0.0.1:6379> keys *
1) "kindom"
2) "name"
3) "king"
4) "pricess"

4.获取数据类型

TYPE key

127.0.0.1:6379> type kindom
zset

5.向尾部添加

APPEND key value

127.0.0.1:6379> append name qqq
(integer) 9
127.0.0.1:6379> get name
"wonderqqq"

6.获取字符串长度

strlen key

127.0.0.1:6379> strlen name
(integer) 9

当然这里只是介绍简单的一些操作,复杂的参考官方文档。

在java应用中使用redis—jedis

前提是redis 已经安装,并且已经开启服务。

jedis 下载地址 https://github.com/xetorthio/jedis

Jedis is a blazingly small and sane Redis java client. Jedis was conceived to be EASY to use. 翻译: jedis是一个非常小的java客户端,被认为是容易使用。

怎么使用?

import redis.clients.jedis.Jedis;

import java.util.List;

public class TestRedisClient {
    public static void main(String[] args){

        Jedis jedis = new Jedis("10.20.112.33");
        System.out.println("Connection to server sucessfully");
        //check whether server is running or not
        System.out.println("Server is running: "+jedis.ping());
        jedis.lpush("wonder-list", "Redis");
        jedis.lpush("wonder-list", "Mongodb");
        jedis.lpush("wonder-list", "Mysql");
        // Get the stored data and print it
        List<String> list = jedis.lrange("wonder-list", 0 ,5);
        for(int i=0; i<list.size(); i++) {
            System.out.println("Stored string in redis:: "+list.get(i));
        }

    }
}
Connection to server sucessfully
Server is running: PONG
Stored string in redis:: Mysql
Stored string in redis:: Mongodb
Stored string in redis:: Redis

以上均为自己安装测试通过。

如果未开启服务,那么则会拒绝连接:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at TestRedisClient.main(TestRedisClient.java:11)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more

其中遇到的问题有:

1.如何通过远程,Windows使用远程linux上的redis服务。

1.注释掉你的redis.conf 的bind ip 的配置 我这里将原来的127.0.0.1注释掉

2.设置 protect-mode no

3.关闭redis服务:redis-server stop 或者使用 ctrl + c

4.启动: redis-server 你的redis.conf 目录 如 /user/redis/src/redis-server /user/redis/redis.conf

启动的时候,可以指定配置文件启动

成功连接。