redis(windows下)


#download

      1)链接:http://pan.baidu.com/s/1sk0HzrB 

      2)密码:adjw

#命令行运行(win8.1为例)

      1)进入解压目录 

       #cd/....

       redis-server.exe redis.conf

       redis-cli.exe -h 127.0.0.1 -p 6379

#常用指令

set lu jianing

get lu

src/redis-cli

停止redis服务:

src/redis-cli shutdown

增删改查:

keys *

取出当前匹配的所有key

> exists larry

(integer) 0

当前的key是否存在

del lv

删除当前key

expire

设置过期时间

> expire larry 10

(integer) 1

> move larry ad4

(integer) 1

移动larry键值对到ad4数据库

> persist lv

(integer) 1

移除当前key的过期时间

randomkey

随机返回一个key

rename

重命名key

type

返回值的数据类型

type testlist

list

> ping

PONG

测试连接是否还在

>echo name

"larry"

打印

> select ad4databank

OK

数据库切换

> quit

退出连接

> dbsize

(integer) 12

当前数据库中key的数量

> info

服务器基本信息

monitor

实时转储收到的请求

config get

获取服务器的参数配置

flushdb

清空当前数据库

flushall

清除所有数据库

#相关代码

<dependency>

        <groupId>redis.clients</groupId>

        <artifactId>jedis</artifactId>

        <version>2.6.2</version>

</dependency>

//构建redis连接池

private staticJedisPool   pool=null;

public staticJedisPool getPool() {

            if(pool==null) {

                   JedisPoolConfig config =newJedisPoolConfig();

                  config.setTestOnBorrow(true);

                  pool=newJedisPool(config,"127.0.0.1",6379,2000);

             }

           returnpool;

}

//获取数据

public staticObject get(String key) {

      Object value =null;

      JedisPool pool =null;

      Jedis jedis =null;

     try{

           pool =getPool();

            jedis = pool.getResource();

           byte[] obj = jedis.get(key.getBytes());

           value = SerializeUtil.unserialize(obj);

    }catch(Exception e) {

//释放redis对象

          pool.returnBrokenResource(jedis);

         e.printStackTrace();

    }finally{

          //返还到连接池

         returnResource(pool, jedis);

     }

     returnvalue;

}

//储存数据

public static void set(String key, Object value) {

          JedisPool pool =null;

          Jedis jedis =null;

         try{

              pool =getPool();

              jedis = pool.getResource();

              jedis.set(key.getBytes(), SerializeUtil.serialize(value));

         }catch(Exception e) {

               //释放redis对象

               pool.returnBrokenResource(jedis);

               e.printStackTrace();

         }finally{

              //返还到连接池

              returnResource(pool, jedis);

         }

}

//删除数据

public static void  del(String key) {

          JedisPool pool =null;

          Jedis jedis =null;

         try{

                 pool =getPool();

                 jedis = pool.getResource();

                 jedis.del(key.getBytes());

          }catch(Exception e) {

                 //释放redis对象

                 pool.returnBrokenResource(jedis);

                 e.printStackTrace();

          }finally{

                 //返还到连接池

                 returnResource(pool, jedis);

          }

}

//清空

public static void   cleanAll() {

           JedisPool pool =null;

           Jedis jedis =null;

          try{

              pool =getPool();

               jedis = pool.getResource();

              jedis.flushAll();

              System.out.println("清理完毕.....");

           }catch(Exception e) {

                //释放redis对象

                pool.returnBrokenResource(jedis);

                e.printStackTrace();

            }finally{

                 //返还到连接池

                 returnResource(pool, jedis);

            }

}

//返还到连接池

public static void returnResource(JedisPool pool, Jedis redis) {

            if(redis !=null) {

                  pool.returnResource(redis);

            }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,382评论 0 36
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,079评论 19 139
  • 测试类: public class TestRedis { /** * @创建时间:2017-10-26 * @创...
    米特侠阅读 6,705评论 0 0
  • RedisRedis命令参考网址:http://doc.redisfans.com/ Redis 简介Redis ...
    野柳阅读 1,157评论 0 9
  • 一说明天要上学,小宝就表情很痛苦的说,我肚子不舒服,明天也不能好,我说那明天就上不了学了,然后马上就变成这样高兴
    欣赏_a779阅读 179评论 0 0