Redis 字符串

2020-09-26  本文已影响0人  爱折腾的傻小子
Setnx (SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值
// 测试框架 Lumen 7.x 
// SETNX KEY_NAME VALUE
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('setnx', [
   'weather',    // KEY_NAME
   123           // VALUE
]);
// or 使用下面形式设置
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->setnx('weather', 123);
// 返回值$return 布尔值 设置成功返回 true, 设置失败返回 false
Msetnx 所有给定 key 都不存在时,同时设置一个或多个 key-value 对
// 如果设置其中有一个key存在那么全部都不会设置成功
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('msetnx', [
    [
        's1'    => 's1',
        's2'    => 's2',
//        'as'    => 'as',
    ]
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->msetnx(        [
//    's1'    => 's1',
//    's2'    => 's2',
//    'as'    => 'as',
//]);
// 返回值设置成功true 设置失败false
SetEx 命令为指定的 key 设置值及其过期时间
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('setex', [
    's1',     // KEY_NAME
    60,       // 过期时间(TIMEOUT)
    'adasd'   // vaule 
]);
// or 
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->setex('s1', 60, 'adasd');
// 返回值$return 布尔值 设置成功返回 true 设置失败返回 false
Psetex 以毫秒为单位设置 key 的生存时间
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('psetex', [
    'num1', 20000, 856
]);
// or
$return = \Illuminate\Support\Facades\Redis::connection('expired')->psetex('num1', 20000, 856);
// 返回值$return 布尔值 设置成功返回 true 设置失败返回 false
GetRange 获取存储在指定 key 中字符串的子字符串
// weather 在redis保存的值是 123  0=>1、1=>2、2=>3
$a = \Illuminate\Support\Facades\Redis::connection('expired')->command('getrange', [
        'weather',   // key
        1,           //   start
        2            // end
]);  // 23
// or 
// \Illuminate\Support\Facades\Redis::connection('expired')->getrange('weather', 1, 2);
// 如果key不存在 或者偏移量不在字符串范围 都返回空串 ""
Setrange
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('setrange', [
    'as', 1, 'ccccc'
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->setrange('as', 1, 'ccccc');
// 返回值返回替换后字符串的长度 失败返回false
Set 设置给定 key 的值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('set', [
    'as', '56789'
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->set('as', '56789');
// 返回值$return返回布尔值 成功返回true 失败返回false
Mset 同时设置一个或多个 key-value
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('mset', 
    [
        ['s1' => '60', 'adasd' => '56']  // 设置key1 => value1
    ]
);
// or 
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->mset([
//    's1' => '61', 'adasd' => '57'
// ]);
// 返回值$return返回布尔值 成功返回true 失败返回false
Get 获取指定 key 的值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('get', [
    'as'  // KEY_NAME
]);
// or
$return = \Illuminate\Support\Facades\Redis::connection('expired')->get('as');
// 返回值$return返回布尔值 成功返回redis存储值 失败返回false
Mget 返回所有(一个或多个)给定 key 的值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('mget', [
    ['num', 'as','ssss']
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->mget(['num', 'as','ssss']);
// 返回值["-28", "56789", null] 不存在的key返回null 失败时返回false
Decr 将 key 中储存的数字值减一
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('decr', ['num']);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->decr('num');
// 返回值 成功返回当前被减一在redis里保存的数 失败返回false 会被减成负数
Decrby key 所储存的值减去给定的减量值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('decrby', ['num', 5]);
// or
$return = \Illuminate\Support\Facades\Redis::connection('expired')->decrby('num', 10);
// 返回值 成功返回当前被减一在redis里保存的数 失败返回false 会被减成负数
Strlen 获取指定 key 所储存的字符串值的长度
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('strlen', [
    'num'
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->strlen('num');
// 失败返回false 成功返回长度 -29 返回 3
Incr 将 key 中储存的数字值增一
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('incr', ['num']);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->incr('num');
// 修改成功返回修改后redis里存储的值 否则返回false
Incrby 将 key 中储存的数字加上指定的增量值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('incrby', ['num', 10]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->incrby('num', 2);
// 修改成功返回修改后redis里存储的值 否则返回false
Incrbyfloat 为 key 中所储存的值加上指定的浮点数增量值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('incrbyfloat', [
    'num', 10.2
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->incrbyfloat('num', 2.3);
// 修改成功返回修改后redis里存储的值 否则返回false
Append 用于为指定的 key 追加值
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('append', [
    'as', '- ccccc'
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->append('as', '- ccccc');
// 成功返回当前更改后redis里面的字符串长度 失败返回false
Setbit 用于对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('setbit', [
    'num',      // Key 
    10086,    // 设置第bit位
    1             // 设置值 1
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->setbit('num', 10086, 1);
// 返回值 0 或者 1 偏移量原来储存的位
Getbit 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
$return = \Illuminate\Support\Facades\Redis::connection('expired')->command('getbit', [
    'num', 2
]);
// or
// $return = \Illuminate\Support\Facades\Redis::connection('expired')->getbit( 'num', 2);
// 返回 字符串值指定偏移量上的位(bit)
// 当偏移量 OFFSET 比字符串值的长度大,或者 key 不存在时,返回 0
上一篇 下一篇

猜你喜欢

热点阅读