欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java连接redis及基本操作示例

程序员文章站 2023-12-18 15:51:34
本文实例讲述了java连接redis及基本操作。分享给大家供大家参考,具体如下: 点击此处:本站下载安装。 解压安装 启动redis:使用cd命令切换目录到 d:...

本文实例讲述了java连接redis及基本操作。分享给大家供大家参考,具体如下:

点击此处:本站下载安装。

解压安装

启动redis:使用cd命令切换目录到 d:\redis运行redis-server.exe redis.windows.conf

默认端口为6379

访问:切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379

pom.xml:

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>
 <groupid>testredis</groupid>
 <artifactid>testredis</artifactid>
 <version>0.0.1-snapshot</version>
 <build/>
 <dependencies>
  <!-- redis nosql 操作依赖 -->
    <dependency>
      <groupid>redis.clients</groupid>
      <artifactid>jedis</artifactid>
      <version>2.1.0</version>
    </dependency>
 </dependencies>
</project>

测试代码:

package com.crisy.redis;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import java.util.set;
import redis.clients.jedis.jedis;
import redis.clients.jedis.jedispool;
import redis.clients.jedis.jedispoolconfig;
import redis.clients.jedis.jedisshardinfo;
import redis.clients.jedis.shardedjedis;
import redis.clients.jedis.shardedjedispool;
public class redisclient {
 /**
 * 一般项目基本都使用非切片池;
 * 切片池主要用于分布式项目,可以设置主从redis库
 */
 private jedis jedis;//非切片客户端连接
 private jedispool jedispool;//非切片连接池
 private shardedjedis shardedjedis;//切片客户端连接
 private shardedjedispool shardedjedispool;//切片连接池
 
 public redisclient(){ 
    initialpool(); //初始化非切片连接池
    initialshardedpool(); //初始化切片连接池
    shardedjedis = shardedjedispool.getresource(); 
    jedis = jedispool.getresource(); 
  }
 private void initialpool() {
 jedispoolconfig config = new jedispoolconfig();
 config.setmaxactive(20);
 config.setmaxidle(5);
 config.setmaxwait(100001);
 config.settestonborrow(false);
 jedispool = new jedispool(config,"127.0.0.1",6379);
 } 
 
 private void initialshardedpool() {
 jedispoolconfig config = new jedispoolconfig();
 config.setmaxactive(20);
 config.setmaxidle(5);
 config.setmaxwait(100001);
 config.settestonborrow(false);
 //slave连接
 list<jedisshardinfo> shardinfos = new arraylist<jedisshardinfo>();
 shardinfos.add(new jedisshardinfo("127.0.0.1",6379,"master"));
 //构造池
 shardedjedispool = new shardedjedispool(config, shardinfos);
 }
 
 public void show(){
 system.out.println("----对value操作的命令----");
 valueoperate();
 system.out.println("----对string操作的命令----");
 stringoperator();
 system.out.println("----对list操作的命令----");
 listoperator();
 system.out.println("----对set操作的命令----");
 setoperator();
 system.out.println("----对hash操作的命令----");
 hashoperate();
 }
 
 
 /**
 * 对value操作的命令
 * @throws interruptedexception 
 */
 private void valueoperate(){
 system.out.println("清空当前选择库中所有key:"+jedis.flushdb());
 system.out.println("清空所有库中所有key:"+jedis.flushall());
 system.out.println("判断mykey键是否存在:"+shardedjedis.exists("mykey")); 
 system.out.println("新增mykey:"+shardedjedis.set("mykey","myvalue")); 
 system.out.println("判断mykey键是否存在:"+shardedjedis.exists("mykey")); 
 system.out.println("查看mykey所储存的值的类型:"+jedis.type("mykey"));
 system.out.println("重命名mykey-mykey_1:"+jedis.rename("mykey", "mykey_1")); 
 system.out.println("判断mykey键是否存在:"+shardedjedis.exists("mykey")); 
 system.out.println("判断mykey_1键是否存在:"+shardedjedis.exists("mykey_1")); 
 system.out.println("新增mykey_2:"+shardedjedis.set("mykey_2","myvalue_2")); 
 system.out.println("查看索引为0的key:"+jedis.select(0));
 system.out.println("当前数据库中key的数目:"+jedis.dbsize());
 system.out.println("系统中所有键如下:");
 //返回满足给定pattern的所有key
    set<string> keys = jedis.keys("*"); 
    iterator<string> it=keys.iterator() ;  
    while(it.hasnext()){  
      string key = it.next();  
      system.out.println(key);  
    }
    //删除时,如果不存在忽略此命令
    system.out.println("系统中删除mykey_3: "+jedis.del("mykey_3"));
    system.out.println("系统中删除mykey_2: "+jedis.del("mykey_2"));
    system.out.println("判断mykey_2是否存在:"+shardedjedis.exists("mykey_2"));
    system.out.println("设置 mykey_1的过期时间为5秒:"+jedis.expire("mykey_1", 5));
    try {
  thread.sleep(2000);
 } catch (interruptedexception e) {
  // todo auto-generated catch block
  e.printstacktrace();
 } 
    //没有设置过期时间或者不存在的都返回-1
    system.out.println("获得mykey_1的剩余活动时间:"+jedis.ttl("mykey_1"));
    system.out.println("取消设置mykey_1的活动时间:"+jedis.persist("mykey_1"));
    system.out.println("查看mykey_1的剩余活动时间:"+jedis.ttl("mykey_1"));
 }
 
 /**
 * 对string操作的命令
 */
 private void stringoperator(){
 system.out.println("清空当前选择库中所有key:"+jedis.flushdb());
 system.out.println("给数据库中名称为key01的string赋予值value:"+jedis.set("key01", "value01"));
 system.out.println("给数据库中名称为key02的string赋予值value:"+jedis.set("key02", "value02"));
 system.out.println("给数据库中名称为key03的string赋予值value:"+jedis.set("key03", "value03"));
 system.out.println("返回数据库中名称为key01的string的value:"+jedis.get("key01"));
 system.out.println("返回库中多个string的value:"+jedis.mget("key01","key02","key03"));
 system.out.println("添加string,名称为key,值为value:"+jedis.setnx("key04","value04"));
 system.out.println("向库中添加string,设定过期时间time:"+jedis.setex("key05",5,"value05"));
 system.out.println("批量设置多个string的值:"+jedis.mset("key06_1","value06_1","key06_2","value06_2"));
 system.out.println("给数据库中名称为key07的string赋予值value:"+jedis.set("key07", "1"));
 system.out.println("名称为key07的string增1操作:"+jedis.incr("key07"));
 //err value is not an integer or out of range
 //system.out.println("名称为key01的string增1操作:"+jedis.incr("key01"));
 system.out.println("名称为key07的string增加integer:"+jedis.incrby("key07", 5));
 system.out.println("名称为key07的string减1操作:"+jedis.decr("key07"));
 system.out.println("名称为key07的string减少integer:"+jedis.decrby("key07", 3));
 system.out.println("名称为key的string的值附加value:"+jedis.append("key01","hello world"));
 system.out.println("返回名称为key的string的value的子串:"+jedis.substr("key01",0,5));
 }
 
 /**
 * 对list操作的命令
 */
 private void listoperator(){
 system.out.println("清空当前选择库中所有key:"+jedis.flushdb());
 //准备两个list
 shardedjedis.lpush("mylist", "java"); 
    shardedjedis.lpush("mylist", "c"); 
    shardedjedis.lpush("mylist", "c++");
    shardedjedis.lpush("mylist", "java");
 system.out.println("在名称为mylist的list尾添加一个值为value的元素:"+jedis.rpush("mylist","phython"));
 system.out.println("在名称为mylist的list头添加一个值为value的 元素:"+jedis.rpush("mylist","php"));
 system.out.println("返回名称为mylist的list的长度:"+jedis.llen("mylist"));
 system.out.println("返回名称为mylist中start至end之间的元素:"+jedis.lrange("mylist",0,3));
 system.out.println("截取名称为mylist的list:"+jedis.ltrim("mylist",0,3));
 system.out.println("返回名称为mylist的list中index位置的元素:"+jedis.lindex("mylist",0));
 system.out.println("给名称为mylist的list中index位置的元素赋值:"+jedis.lset("mylist",0,"hahahahah我改啦"));
 system.out.println("删除count个mylist的key中值为value的元素:"+jedis.lrem("mylist",1,"java"));
 system.out.println("返回并删除名称为mylist的list中的首元素:"+jedis.lpop("mylist"));
 system.out.println("返回并删除名称为mylist的list中的尾元素:"+jedis.rpop("mylist"));
 }
 
 /**
 * 对set操作的命令
 */
 private void setoperator(){
 system.out.println("清空当前选择库中所有key:"+jedis.flushdb());
 //准备两个set
 jedis.sadd("myset","hahaha");
 jedis.sadd("myset","hahaha");
 jedis.sadd("myset","lalala");
 jedis.sadd("myset","kakaka");
 jedis.sadd("myset2","hahaha");
 jedis.sadd("myset2","lalala");
 jedis.sadd("myset2","kakaka");
 system.out.println("向名称为myset的set中添加元素member:"+jedis.sadd("myset","wawawa"));
 system.out.println("返回名称为myset的set的所有元素:"+jedis.smembers("myset"));
 system.out.println("删除名称为myset的set中的元素member:"+jedis.srem("myset","wawawa"));
 system.out.println("返回名称为myset的set的所有元素:"+jedis.smembers("myset"));
 system.out.println("随机返回并删除名称为myset的set中一个元素:"+jedis.spop("myset"));
 system.out.println("移到集合元素:"+jedis.smove("myset","myset2","hahaha"));
 system.out.println("返回名称为myset的set的所有元素:"+jedis.smembers("myset"));
 system.out.println("返回名称为myset2的set的所有元素:"+jedis.smembers("myset2"));
 system.out.println("返回名称为mykey的set的基数:"+jedis.scard("myset"));
 system.out.println("member是否是名称为mykey的set的元素:"+jedis.sismember("myset","hahaha"));
 system.out.println("求交集:"+jedis.sinter("myset","myset2"));
 system.out.println("求交集并将交集保存到dstkey的集合:"+jedis.sinterstore("dstkey","myset2","myset"));
 system.out.println("返回名称为dstkey的set的所有元素:"+jedis.smembers("dstkey"));
 system.out.println("求并集:"+jedis.sunion("myset","myset2"));
 system.out.println("求并集并将并集保存到dstkey的集合:"+jedis.sinterstore("dstkey","myset2","myset"));
 system.out.println("返回名称为dstkey的set的所有元素:"+jedis.smembers("dstkey"));
 system.out.println("求差集:"+jedis.sdiff("myset2","myset"));
 system.out.println("求差集并将差集保存到dstkey的集合:"+jedis.sdiffstore("dstkey","myset2","myset"));
 system.out.println("返回名称为dstkey的set的所有元素:"+jedis.smembers("dstkey"));
 system.out.println("随机返回名称为dstkey的set的一个元素:"+jedis.srandmember("dstkey"));
 }
 
 /**
 * hash
 */
 private void hashoperate(){
 system.out.println(jedis.flushdb());
 system.out.println("向名称为key的hash中添加元素field1:"+shardedjedis.hset("key", "field1", "value1")); 
    system.out.println("向名称为key的hash中添加元素field2:"+shardedjedis.hset("key", "field2", "value2")); 
    system.out.println("向名称为key的hash中添加元素field3:"+shardedjedis.hset("key", "field3", "value3"));
    system.out.println("返回名称为key的hash中field1对应的value:"+shardedjedis.hget("key", "field1"));
    system.out.println("返回名称为key的hash中field1对应的value:"+shardedjedis.hmget("key", "field1","field2","field3"));
    system.out.println("删除名称为key的hash中键为field1的域:"+shardedjedis.hdel("key", "field1"));
    system.out.println("返回名称为key的hash中所有键对应的value:"+shardedjedis.hvals("key"));
    system.out.println("返回名称为key的hash中元素个数:"+shardedjedis.hlen("key"));
    system.out.println("返回名称为key的hash中所有键:"+shardedjedis.hkeys("key"));
    system.out.println("返回名称为key的hash中所有的键(field)及其对应的value:"+shardedjedis.hgetall("key"));
    system.out.println("名称为key的hash中是否存在键为field1的域:"+shardedjedis.hexists("key","field1"));
 }
}

更多关于java相关内容感兴趣的读者可查看本站专题:《java+mysql数据库程序设计总结》、《java操作excel技巧总结》、《java数据结构与算法教程》、《java文件与目录操作技巧汇总》及《java操作dom节点技巧总结

希望本文所述对大家java程序设计有所帮助。

上一篇:

下一篇: