承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

框架:spring框架如何使用redis

管理员 2023-03-29 08:24:45 互联网圈 40 ℃ 0 评论 6167字 收藏

spring框架如何使用redis

spring框架怎么使用redis

"spring框架使用redis的方法:

1.在pom.xml中导入redis的相干依赖,例如:

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.1.0</version>

</dependency>

2.在applicationContext.xml中添加redis相干配置,例如:

<!--redis 配置 开始-->

<bean id=""jedisPoolConfig"" class=""redis.clients.jedis.JedisPoolConfig"">

<property name=""maxActive"" value=""300""/>

<property name=""maxIdle"" value=""100""/>

<property name=""maxWait"" value=""1000""/>

<property name=""testOnBorrow"" value=""true""/>

</bean>

<!-- Config poolConfig, String host, int port, int timeout, String password, int database-->

<bean id=""jedisPool"" class=""redis.clients.jedis.JedisPool"" destroy-method=""destroy"">

<constructor-arg ref=""jedisPoolConfig""/>

<constructor-arg value=""127.0.0.1""/>

<constructor-arg value=""6379""/>

<constructor-arg value=""3000""/>

<constructor-arg value=""123456""/>

<constructor-arg value=""0""/>

</bean>

<bean id=""redisAPI"" class=""com.xc.util.RedisAPI"">

<property name=""jedisPool"" ref=""jedisPool""/>

</bean>

3.最后创建redis的工具类便可。代码以下:

public class RedisAPI {

public static JedisPool jedisPool;

public JedisPool getJedisPool() {

return jedisPool;

}

public void setJedisPool(JedisPool jedisPool) {

RedisAPI.jedisPool = jedisPool;

}

/**

* set key and value to redis

* @param key

* @param value

* @return

*/

public static boolean set(String key,String value){

try{

Jedis jedis = jedisPool.getResource();

jedis.set(key, value);

return true;

}catch(Exception e){

e.printStackTrace();

}

return false;

}

/**

* set key and value to redis

* @param key

* @param seconds 有效期

* @param value

* @return

*/

public static boolean set(String key,int seconds,String value){

try{

Jedis jedis = jedisPool.getResource();

jedis.setex(key, seconds, value);

return true;

}catch(Exception e){

e.printStackTrace();

}

return false;

}

/**

* 判断某个key是否是存在

* @param key

* @return

*/

public boolean exist(String key){

try{

Jedis jedis = jedisPool.getResource();

return jedis.exists(key);

}catch(Exception e){

e.printStackTrace();

}

return false;

}

/**

* 返还到连接池

* @param pool

* @param redis

*/

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

if(redis != null){

pool.returnResource(redis);

}

}

/**

* 获得数据

* @param key

* @return

*/

public String get(String key){

String value = null;

Jedis jedis = null;

try{

jedis = jedisPool.getResource();

value = jedis.get(key);

}catch(Exception e){

e.printStackTrace();

}finally{

//返还到连接池

returnResource(jedisPool, jedis);

}

return value;

}

/**

* 查询key的有效期,当 key 不存在时,返回 ⑵ 。 当 key 存在但没有设置剩余生存时间时,返回 ⑴ 。 否则,以秒为单位,返回 key 的剩余生存时间。

* 注意:在 Redis 2.8 之前,当 key 不存在,还是 key 没有设置剩余生存时间时,命令都返回 ⑴ 。

* @param key

* @return 剩余多少秒

*/

public Long ttl(String key){

try{

Jedis jedis = jedisPool.getResource();

return jedis.ttl(key);

}catch(Exception e){

e.printStackTrace();

}

return (long) ⑵;

}

/**

* 删除

* @param key

*/

public void delete(String key){

try{

Jedis jedis = jedisPool.getResource();

jedis.del(key);

}catch(Exception e){

e.printStackTrace();

}

}

文章来源:丸子建站

文章标题:框架:spring框架如何使用redis

https://www.wanzijz.com/view/5167.html

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信