redis的安装及使用

本文详述了Redis在Windows和CentOS上的安装步骤,包括Java环境的配置,并提供了Redis命令示例。此外,还展示了在LNMP环境中集成Redis的方法,以及PHP和JAVA代码进行Redis操作的测试示例,适合开发者快速入门和参考。
摘要由CSDN通过智能技术生成

redis


redis笔记分享: redis运用与引用.

今天打开老项目运行,又是一如既往的报错。解决完几个卡点后,报了redis的异常,幸好之前做了笔记,很快就搞定了。觉得还是有些用处的,就把它分享出来给大家,一起学习进步。
在这里插入图片描述

windows安装redis

https://github.com/MicrosoftArchive/redis/releases

  • cmd分开运行
  • 服务端
    redis-server.exe redis.windows.conf
  • 客户端 redis-cli.exe -h 127.0.0.1 -p 6379

centos 安装java及redis

$yum install java java-1.8.0-openjdk-devel

$ wget http://download.redis.io/releases/redis-5.0.4.tar.gz
$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make

查看进程

ps -ef | grep rabbit
root     14471 14443  0 14:37 pts/1    00:00:00 grep --color=auto rabbit
//杀死进程
kill -9 14471

centos redis运行(已更改配置)

[root@izuf6hbxeno029okk0ukaoz config]# /usr/local/bin/redis-server /home/java/config/redis.conf 

[root@izuf6hbxeno029okk0ukaoz config]# ps aux|grep redis.server

[root@izuf6hbxeno029okk0ukaoz redis-5.0.4]# redis-cli -h 127.0.0.1 -p 7200

redis命令

info
查看redis信息


//string类型

127.0.0.1:7200> set string1 liuchaotan
OK
127.0.0.1:7200> get string1
"liuchaotan"
127.0.0.1:7200> 
127.0.0.1:7200> 
127.0.0.1:7200> set string2 4
OK
127.0.0.1:7200> get string2
"4"
//自增
127.0.0.1:7200> incr string2
(integer) 5
127.0.0.1:7200> get string2
"5"
127.0.0.1:7200> decrby string2 3
(integer) 2
127.0.0.1:7200> 

//list (队列的实现)
//从左边插入,元素可以重复

127.0.0.1:7200> lpush list1 12
(integer) 1
127.0.0.1:7200> lpush list1 13
(integer) 2
127.0.0.1:7200> rpop list1
"12"
127.0.0.1:7200> 
127.0.0.1:7200> lpush list2 12
(integer) 1
127.0.0.1:7200> lpush list2 13
(integer) 2
127.0.0.1:7200> lpush list2 13
(integer) 3
//查看数量
127.0.0.1:7200> llen list2
(integer) 3
127.0.0.1:7200> 

//set

127.0.0.1:7200> sadd set1 12
(integer) 1
127.0.0.1:7200> scard set1
(integer) 1
127.0.0.1:7200> sadd set1 13
(integer) 1
127.0.0.1:7200> sadd set1 13
(integer) 0
127.0.0.1:7200> sadd set1 13
(integer) 0
127.0.0.1:7200> scard set1
(integer) 2
127.0.0.1:7200> sismember set1 13
(integer) 1
127.0.0.1:7200> srem set1 13
(integer) 1
127.0.0.1:7200> sismember set1 13
(integer) 0

//hash类型
127.0.0.1:7200> hset hash1 key1 12
(integer) 1
127.0.0.1:7200> hget hash1 key1
"12"
127.0.0.1:7200> hset hash1 key1 13
(integer) 0
127.0.0.1:7200> hset hash1 key2 13
(integer) 1
127.0.0.1:7200> hset hash1 key3 13
(integer) 1
127.0.0.1:7200> hlen hash1
(integer) 3
127.0.0.1:7200> hset hash1 key3 14
(integer) 0
127.0.0.1:7200> hget hash1 key3
"14"
127.0.0.1:7200> hget hash1 key1
"13"
127.0.0.1:7200> hmget hash1 key1 key2 key3
1) "13"
2) "13"
3) "14"

//sort set 分数与元素的映射
//先按sort 排序再按value排序

127.0.0.1:7200> zadd zset 10.1 val1
(integer) 1
127.0.0.1:7200> zadd zset 11.2 val2
(integer) 1
127.0.0.1:7200> zadd zset 9.2 val3
(integer) 1
127.0.0.1:7200> zcard zset
(integer) 3
127.0.0.1:7200> zrange zset 0 2 withscores
1) "val3"
2) "9.1999999999999993"
3) "val1"
4) "10.1"
5) "val2"
6) "11.199999999999999"
//查看排序
127.0.0.1:7200> zrank zset val2
(integer) 2
127.0.0.1:7200> zadd zset 12.2 val3
(integer) 0
127.0.0.1:7200> zrange zset 0 2 withscores
1) "val1"
2) "10.1"
3) "val2"
4) "11.199999999999999"
5) "val3"
6) "12.199999999999999"
127.0.0.1:7200> zadd zset 12.2 val2
(integer) 0
127.0.0.1:7200> zrange zset 0 2 withscores
1) "val1"
2) "10.1"
3) "val2"
4) "12.199999999999999"
5) "val3"
6) "12.199999999999999"
127.0.0.1:7200> 

lnmp安装使用redis


cd lnmp1.5
#执行
./addons.sh install redis
#直接回车安装最新稳定版本

vi /usr/local/redis/etc/redis.conf

//外网
修改 /usr/local/redis/etc/redis.conf 里将bind 127.0.0.1 改成 bind 0.0.0.0 重启redis 
还需要将防火墙里redis的端口 6379的禁止访问规则去掉

php代码测试

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('name','zhou', 10);
$key_1 = $redis->get('name');
echo $key_1;
?>

JAVA代码测试

  public static void main(String[] args) throws Exception {
    Jedis j = new Jedis(hnp);
    j.connect();
//    j.auth("foobared");
    j.flushAll();
    j.quit();
    j.disconnect();
    long t = System.currentTimeMillis();
    // withoutPool();
//    withPool();
    long elapsed = System.currentTimeMillis() - t;
//    System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops");

    j.set("liu","6");
    
    String  res = j.get("liu");
    System.out.println(res);
  }

Java私人框架

pox.xml

<dependency>
    <groupId>com.wings60</groupId>
    <artifactId>wings60-fundamental-redis</artifactId>
</dependency>



package com.wings.bb.steward.redis;


import com.wings60.fundamental.common.cache.CacheKeyGenerator;
import com.wings60.fundamental.common.utils.StrUtils;
import com.wings60.fundamental.common.utils.TimeUtils;
import com.wings60.fundamental.redis.RedisConnector;

/**
 * 用户redis缓存
 * Created by brucezee on 2017/5/24.
 */
public class UserRedisCache {

    public static final CacheKeyGenerator generator = new CacheKeyGenerator("bb.steward.");

    private UserRedisCache() {
    }

//    public static void main(String[] args) {
//        RedisConnector
//    }
    /**
     * 一个账号每天登录失败限制缓存key
     *
     * @param phone
     * @param ip
     * @return
     */
    public static String processLoginAccountFailedCountCacheKey(String phone, String ip) {
        String parameters = generator.append(phone, ip, TimeUtils.getTodayDayCount()).toString();
        return generator.generate("login_failed", StrUtils.getMd5(parameters));
    }

    /**
     * 获取一个账号每天登录失败次数
     *
     * @param phone
     * @param ip
     * @return
     */
    public static Integer getLoginAccountFailedCount(String phone, String ip) {
        String cacheKey = processLoginAccountFailedCountCacheKey(phone, ip);
        return RedisConnector.getInt(cacheKey);
    }

    /**
     * 设置或增加一个额账号每天登录失败次数
     *
     * @param phone
     * @param ip
     */
    public static void setOrIncreaseLoginAccountFailedCount(String phone, String ip) {
        String cacheKey = processLoginAccountFailedCountCacheKey(phone, ip);
        //设置失败次数
        if (!RedisConnector.exists(cacheKey)) {
            RedisConnector.set(cacheKey, TimeUtils.getTodayLeftMillis(), "1");
        } else {
            RedisConnector.incrBy(cacheKey, 1);
        }
    }

    /**
     * 删除每天登录失败次数
     *
     * @param phone
     */
    public static void delUserRemoteLoginInfo(String phone, String ip) {
        String cacheKey = processLoginAccountFailedCountCacheKey(phone, ip);
        RedisConnector.del(cacheKey);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值