jedis相关详解
一、jedis是什么?
Jedis是redis的java版本的客户端实现,使用Jedis提供的Java API对Redis进行操作,是Redis官方推崇的方式;并且,使用Jedis提供的对Redis的支持也最为灵活、全面;不足之处,就是编码复杂度较高。
二、hello world
1、下载jar包,使用maven的话添加如下依赖即可
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、建立连接
Jedis jedis = new Jedis("127.0.0.1", 6379);
3、jedis操作
- String操作
jedis.set("username", "张三疯了");
System.out.println(jedis.get("username"));
jedis.append("username", "吗?");
System.out.println(jedis.get("username"));
jedis.del("username");
System.out.println(jedis.get("username"));
jedis.mset("username", "zhangsan", "sex", "男", "age", "18");
jedis.incr("age");
System.out.println(jedis.mget("username", "sex", "age"));
- Map操作
Map<String, String> person = new HashMap<>();
person.put("name", "李四");
person.put("sex", "男");
person.put("age", "28");
jedis.hmset("person", person);
System.out.println(jedis.hmget("person", "name", "sex", "age"));
jedis.hdel("person", "age");
System.out.println(jedis.hmget("person", "age"));
System.out.println(jedis.hlen("person"));
System.out.println(jedis.exists("person"));
System.out.println(jedis.hkeys("person"));
System.out.println(jedis.hvals("person"));
System.out.println(jedis.hgetAll("person"));
- List操作
jedis.lpush("persons", "张三", "李四", "王五");
System.out.println(jedis.lrange("persons", 0, -1));
jedis.del("persons");
jedis.rpush("persons", "张三", "李四", "王五");
System.out.println(jedis.lrange("persons", 0, -1));
jedis.del("li");
jedis.lpush("li", "10");
jedis.lpush("li", "15");
jedis.rpush("li", "66");
jedis.rpush("li", "44");
jedis.rpush("li", "99");
System.out.println(jedis.lrange("li", 0, -1));
System.out.println(jedis.sort("li"));
System.out.println(jedis.lrange("li", 0, -1));
- Set操作
jedis.sadd("users", "张三","李四","张三","王五");
System.out.println(jedis.scard("users"));
System.out.println(jedis.smembers("users"));
jedis.srem("users", "张三");
System.out.println(jedis.smembers("users"));
System.out.println(jedis.sismember("users", "李四"));
System.out.println(jedis.srandmember("users"));
三、spring整合
1、添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.5.RELEASE</version>
</dependency>
2、创建redisdb.properties文件,配置redis相关参数
redis.host=127.0.0.1
redis.port=6379
#redis.pass=123456
redis.maxIdle=500
redis.maxTotal=600
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.weight=1
redis.timeout=3000
maxIdle:jedis最大活跃数
maxTotal :jedis最大连接数
minIdle : jedis最小空闲数(连接池中最少保留的jedis连接数)
testOnBorrow :在borrow一个jedis的时候验证其是否可用,可用则返回,不可用则废弃重新创建一个
3、在spring配置文件中添加redis配置
<context:property-placeholder location="classpath:redisdb.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.host}"></constructor-arg>
<constructor-arg index="1" value="${redis.port}" type="int"></constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
4、测试
更多推荐
所有评论(0)