java redis赋值_java项目中使用redis

本文介绍了如何在Java项目中使用Redis,包括引入jedis和commons-pool2 jar包,设置JedisPool配置,并提供了一个JedisPool连接池的示例代码,展示了如何在多线程环境中安全高效地使用Redis。
摘要由CSDN通过智能技术生成

1.具体如何下载redis安装包及安装到操作系统上,这个可以去百度搜索

2.安装redis后,启动redis服务

下面主要介绍如何在java项目中使用redis

1.项目中引入jar包:jedis.jar,commons-pool2.jar,jar包的版本具体是多少,可以去官网或百度上搜索下载,由于我的的项目使用了maven,所以直接配置:

188175081_1_20200415102420463

2.下面是我写的一个简单的demo,源码已经全部粘贴出来了

package com.jlvc.test;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class RedisTest {

private static JedisPool jedisPool;// Jedis连接池

public static JedisPool

getPool() {

if (jedisPool == null) {

JedisPoolConfig config = new JedisPoolConfig();

//

控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;

//

如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。

//config.setMaxActive(500);

config.setMaxTotal(500);

//

控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。

config.setMaxIdle(5);

//

表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;

config.setMaxWaitMillis(1000 * 100);

//

config.setMaxWait(1000 * 100);

//

在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;

config.setTestOnBorrow(true);

jedisPool

= new JedisPool(config, "127.0.0.1", 6379);

}

return jedisPool;

}

public static void

returnResource(JedisPool pool, Jedis redis) {

if (redis != null) {

//

pool.returnResource(redis);

redis.close();

}

}

public static String

getString(String key) {

String value = null;

JedisPool pool = null;

Jedis jedis = null;

try {

pool =

getPool();

jedis =

pool.getResource();

value =

jedis.get(key);

} catch (Exception e) {

//

释放redis对象

//pool.returnBrokenResource(jedis);

e.printStackTrace();

if(jedis!=null){

jedis.close();

}

} finally {

//

返还到连接池

returnResource(pool, jedis);

}

return value;

}

public static boolean

setString(String key, String value) {

JedisPool pool = null;

Jedis jedis = null;

try {

pool =

getPool();

jedis =

pool.getResource();

value =

jedis.set(key, value);

} catch (Exception e) {

//

释放redis对象

//pool.returnBrokenResource(jedis);

e.printStackTrace();

if(jedis!=null){

jedis.close();

}

return

false;

} finally {

//

返还到连接池

returnResource(pool, jedis);

}

return true;

}

public static void main(String[] args) throws

UnsupportedEncodingException {

JedisPool pool=RedisTest.getPool();

Jedis jedis=pool.getResource();

System.out.println("连接成功!");

System.out.println("redis服务正在运行:"+jedis.ping());

String xiaopang=jedis.get("xiaopang");

System.out.println(StringUtils.isBlank(xiaopang)?"空":xiaopang);

jedis.getSet("test", "redis测试");

System.out.println(jedis.get("test"));

}

}

3.测试结果如下图

188175081_2_20200415102420541

4.简单说明:

Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口。

Jedis不是线程安全的,故不应该在多线程环境中共用一个Jedis实例。但是,也应该避免直接创建多个Jedis实例,因为这种做法会导致创建过多的socket连接,性能不高。

要保证线程安全且获得较好的性能,可以使用JedisPool。JedisPool是一个连接池,既可以保证线程安全,又可以保证了较高的效率。

可以声明一个全局的JedisPool变量来保存JedisPool对象的引用,然后在其他地方使用。要知道,JedisPool是一个线程安全的连接池。1.具体如何下载redis安装包及安装到操作系统上,这个可以去百度搜索

2.安装redis后,启动redis服务

下面主要介绍如何在java项目中使用redis

1.项目中引入jar包:jedis.jar,commons-pool2.jar,jar包的版本具体是多少,可以去官网或百度上搜索下载,由于我的的项目使用了maven,所以直接配置:

188175081_1_20200415102420463

2.下面是我写的一个简单的demo,源码已经全部粘贴出来了

package com.jlvc.test;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;

import redis.clients.jedis.JedisPool;

import redis.clients.jedis.JedisPoolConfig;

public class RedisTest {

private static JedisPool jedisPool;// Jedis连接池

public static JedisPool

getPool() {

if (jedisPool == null) {

JedisPoolConfig config = new JedisPoolConfig();

//

控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;

//

如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。

//config.setMaxActive(500);

config.setMaxTotal(500);

//

控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。

config.setMaxIdle(5);

//

表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;

config.setMaxWaitMillis(1000 * 100);

//

config.setMaxWait(1000 * 100);

//

在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;

config.setTestOnBorrow(true);

jedisPool

= new JedisPool(config, "127.0.0.1", 6379);

}

return jedisPool;

}

public static void

returnResource(JedisPool pool, Jedis redis) {

if (redis != null) {

//

pool.returnResource(redis);

redis.close();

}

}

public static String

getString(String key) {

String value = null;

JedisPool pool = null;

Jedis jedis = null;

try {

pool =

getPool();

jedis =

pool.getResource();

value =

jedis.get(key);

} catch (Exception e) {

//

释放redis对象

//pool.returnBrokenResource(jedis);

e.printStackTrace();

if(jedis!=null){

jedis.close();

}

} finally {

//

返还到连接池

returnResource(pool, jedis);

}

return value;

}

public static boolean

setString(String key, String value) {

JedisPool pool = null;

Jedis jedis = null;

try {

pool =

getPool();

jedis =

pool.getResource();

value =

jedis.set(key, value);

} catch (Exception e) {

//

释放redis对象

//pool.returnBrokenResource(jedis);

e.printStackTrace();

if(jedis!=null){

jedis.close();

}

return

false;

} finally {

//

返还到连接池

returnResource(pool, jedis);

}

return true;

}

public static void main(String[] args) throws

UnsupportedEncodingException {

JedisPool pool=RedisTest.getPool();

Jedis jedis=pool.getResource();

System.out.println("连接成功!");

System.out.println("redis服务正在运行:"+jedis.ping());

String xiaopang=jedis.get("xiaopang");

System.out.println(StringUtils.isBlank(xiaopang)?"空":xiaopang);

jedis.getSet("test", "redis测试");

System.out.println(jedis.get("test"));

}

}

3.测试结果如下图

188175081_2_20200415102420541

4.简单说明:

Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口。

Jedis不是线程安全的,故不应该在多线程环境中共用一个Jedis实例。但是,也应该避免直接创建多个Jedis实例,因为这种做法会导致创建过多的socket连接,性能不高。

要保证线程安全且获得较好的性能,可以使用JedisPool。JedisPool是一个连接池,既可以保证线程安全,又可以保证了较高的效率。

可以声明一个全局的JedisPool变量来保存JedisPool对象的引用,然后在其他地方使用。要知道,JedisPool是一个线程安全的连接池。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值