Spring集成jedis的配置与使用简单实例

网友投稿 291 2023-01-11


Spring集成jedis的配置与使用简单实例

jedis是redis的java客户端,spring将redis连接池作为一个bean配置。

redis连接池分为两种,一种是“redis.clients.jedis.ShardedJedisPool”,这是基于hash算法的一种分布式集群redis客户端连接池。

另一种是“redis.clients.jedis.JedisPool”,这是单机环境适用的redis连接池。

maven导入相关包:

redis.clients

jedis

2.9.0

ShardedJedisPool是redis集群客户端的对象池,可以通过他来操作ShardedJedis,下面是ShardedJedisPool的xml配置,spring-jedis.xml:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

${redis.pool.maxActive}

${redis.pool.maxIdle}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

${redis.pool.maxActive}

${redis.pool.maxIdle}

下面是单机环境下redis连接池的配置:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

${redis.pool.maxActive}

${redis.pool.maxIdle}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd">

${redis.pool.maxActive}

${redis.pool.maxIdle}

对应的classpath:properties/redis.properties.xml为:

#最大分配的对象数

redis.pool.maxActive=200

#最大能够保持idel状态的对象数

redis.pool.maxIdle=50

redis.pool.minIdle=10

redis.pool.maxWaitMillis=20000

#当池内没有返回对象时,最大等待时间

redis.pool.maxWait=300

#格式:redis://:[密码]@[服务器地址]:[端口]/[db index]

redis.uri = redis://:12345@127.0.0.1:6379/0

redis.host = 127.0.0.1

redis.port = 6379

redis.timeout=30000

redis.password = 12345

redis.database = 0

二者操作代码类似,都是先注入连接池,然后通过连接池获得jedis实例,通过实例对象操作redis。

ShardedJedis操作:

@Autowired

private ShardedJedisPool shardedJedisPool;//注入ShardedJedisPool

@RequestMapping(value = "/demo_set",method = RequestMethod.GET)

@ResponseBody

public String demo_set(){

//获取ShardedJedis对象

ShardedJedis shardJedis = shardedJedisPool.getResource();

//存入键值对

shardJedis.set("key1","hello jedis");

//回收ShardedJedis实例

shardJedis.close();

return "set";

}

@RequestMapping(value = "/demo_get",method = RequestMethod.GET)

@ResponseBody

public String demo_get(){

ShardedJedis shardedJedis = shardedJedisPool.getResource();

//根据键值获得数据

String result = shardedJedis.get("key1");

shardedJedis.close();

return result;

}

Jedis操作:

@Autowired

private JedisPool jedisPool;//注入JedisPool

@RequestMapping(value = "/demo_set",method = RequestMethod.GET)

@ResponseBody

public String demo_set(){

//获取ShardedJedis对象

Jedis jedis = jedisPool.getResource();

//存入键值对

jedis.set("key2","hello jedis one");

//回收ShardedJedis实例

jedis.close();

return "set";

}

@RequestMapping(value = "/demo_get",method = RequestMethod.GET)

@ResponseBody

public String demo_get(){

Jedis jedis = jedisPool.getResource();

//根据键值获得数据

String result = jedis.get("key2");

jedis.close();

return result;

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试用例怎么读(接口测试用例包含哪些内容)
下一篇:接口测试用例怎么导入禅道(禅道怎么写测试用例)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~