#Redis学习笔记之二#Redis初步使用

Redis启动

上一篇末尾也有提到,Redis启动只需要一个可执行文件redis-server以及一个配置文件redis.config即可。
在redis.config中可以配置有许多启动参数,然而初学无需搞懂所有参数,直接使用默认值启动即可。

启动完毕后可以见到以下“图案”

86036:C 04 Aug 22:14:27.539 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo                                                                 
86036:C 04 Aug 22:14:27.540 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=86036, just started
86036:C 04 Aug 22:14:27.540 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
86036:M 04 Aug 22:14:27.541 * Increased maximum number of open files to 10032 (it was originally set to 256).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.11 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 86036
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

86036:M 04 Aug 22:14:27.543 # Server initialized
86036:M 04 Aug 22:14:27.543 * DB loaded from disk: 0.000 seconds
86036:M 04 Aug 22:14:27.543 * Ready to accept connections

可见Redis已经在6379端口启动成功,进程号为86036

Redis连接

Redis安装完毕后自带redis-cli可以作为命令行连接工具(类比MySQL的mysql命令,Oracle的sqlplus),当然也针对各种编程语言提供驱动,例如Java,Python,C#等。限于篇幅,本文只介绍redis-cli,python和Java的连接方式。

redis-cli

可以通过redis-cli –help查看使用方法,常用的参数有,可以说是非常简明易懂了。

  -h <hostname>      Server hostname (default: 127.0.0.1).
  -p <port>          Server port (default: 6379).
  -a <password>      Password to use when connecting to the server.
  -u <uri>           Server URI.

如果server启动在本机,可以直接运行redis-cli,默认连接到127.0.0.1:6379
连上redis服务器后可以进行测试:

127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"

该命令设置了一条key-value的记录(foo-bar)

Jedis

Jedis是Redis的官方推荐Java客户端:https://redis.io/clients

Maven

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

gradle

compile "redis.clients:jedis:2.9.0"

Java代码相当简洁,使用方法也和redis-cli如出一撤:

public class JedisTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis();
        jedis.set("foo", "bar");
        System.out.println(jedis.get("foo"));
    }
}

redis-py

redis-py也是redis官方推荐的python客户端,用法也是一目了然。

import redis

r = redis.Redis()
r.set('foo', 'bar')
print(r.get('foo'))
    原文作者:arache
    原文地址: https://segmentfault.com/a/1190000015887399
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞