0%

Redis之模块

Redis4.0版本终于加入了模块module,用户可以自己根据需求扩展Redis相关的功能(实现新的Redis命令、数据结构等),基于C99标准,即使用C/C++或者其它语言的C绑定接口均可实现。

一、概念

      Redis4.0版本终于加入了模块module,用户可以自己根据需求扩展Redis相关的功能(实现新的Redis命令、数据结构等),基于C99标准,即使用C/C++或者其它语言的C绑定接口均可实现。它分为系统模块和用户自定义模块,即你可根据自己的需求实现,然后加载进来即可使用。自定义模块如果有没有捕获或内存泄露出错等问题Redis本身是无法处理的,会导致程序整个挂掉,甚至很可能连日志都没有需要非常小心。

二、使用

  1. 启动方式

    • 通过配置文件方式
      • 加入loadmodule /usr/local/your_path/redis-module-panda/panda.so
      • 启动./redis-server 6379.conf
    • 通过命令方式
      • 客户端启动后:127.0.0.1:6379>module load /usr/local/your_path/redis-module-panda/panda.so
      • 服务端启动时:./redis-server 6379.conf --loadmodule ./modules/redis-module-panda/panda.so
  2. panda例子

    • git clone https://github.com/wujunze/redis-module-panda.git,假设目录/usr/local/redis-module-panda
    • cd /usr/local/redis-module-panda
    • make
    • 启动,假设按配置文件方式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    127.0.0.1:6379> module list
    1) 1) "name"
    2) "panda"
    3) "ver"
    4) (integer) 1
    127.0.0.1:6379> panda.hello
    _,add8ba,
    ,d888888888b,
    d8888888888888b _,ad8ba,_
    d888888888888888) ,d888888888b,
    I8888888888888888 _________ ,8888888888888b
    __________`Y88888888888888P"""""""""""baaa,__ ,888888888888888,
    ,adP"""""""""""9888888888P""^ ^""Y8888888888888888I
    ,a8"^ ,d888P"888P^ ^"Y8888888888P'
    ,a8^ ,d8888' ^Y8888888P'
    a88' ,d8888P' I88P"^
    ,d88' d88888P' "b,
    ,d88' d888888' `b,
    ,d88' d888888I `b,
    d88I ,8888888' ___ `b,
    ,888' d8888888 ,d88888b, ____ `b,
    d888 ,8888888I d88888888b, ,d8888b, `b
    ,8888 I8888888I d8888888888I ,88888888b 8,
    I8888 88888888b d88888888888' 8888888888b 8I
    d8886 888888888 Y888888888P' Y8888888888, ,8b
    88888b I88888888b `Y8888888^ `Y888888888I d88,
    Y88888b `888888888b, `""""^ `Y8888888P' d888I
    `888888b 88888888888b, `Y8888P^ d88888
    Y888888b ,8888888888888ba,_ _______ `""^ ,d888888
    I8888888b, ,888888888888888888ba,_ d88888888b ,ad8888888I
    `888888888b, I8888888888888888888888b, ^"Y888P"^ ____.,ad88888888888I
    88888888888b,`888888888888888888888888b, "" ad888888888888888888888'
    8888888888888698888888888888888888888888b_,ad88ba,_,d88888888888888888888888
    88888888888888888888888888888888888888888b,`"""^ d8888888888888888888888888I
    8888888888888888888888888888888888888888888baaad888888888888888888888888888'
    Y8888888888888888888888888888888888888888888888888888888888888888888888888P
    I888888888888888888888888888888888888888888888P^ ^Y8888888888888888888888'
    `Y88888888888888888P88888888888888888888888888' ^88888888888888888888I
    `Y8888888888888888 `8888888888888888888888888 8888888888888888888P'
    `Y888888888888888 `888888888888888888888888, ,888888888888888888P'
    `Y88888888888888b `88888888888888888888888I I888888888888888888'
    "Y8888888888888b `8888888888888888888888I I88888888888888888'
    "Y88888888888P `888888888888888888888b d8888888888888888'
    ^""""""""^ `Y88888888888888888888, 888888888888888P'
    "8888888888888888888b, Y888888888888P^
    `Y888888888888888888b `Y8888888P"^
    "Y8888888888888888P `""""^
    `"YY88888888888P'

三、参考

  1. 参考一
  2. 参考二
  3. 参考三
  4. 参考四
  5. 参考五