1.具体如何下载redis安装包及安装到操作系统上,这个可以去百度搜索
2.安装redis后,启动redis服务
下面主要介绍如何在java项目中使用redis
1.项目中引入jar包:jedis.jar,commons-pool2.jar,jar包的版本具体是多少,可以去官网或百度上搜索下载,由于我的的项目使用了maven,所以直接配置:
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.测试结果如下图
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,所以直接配置:
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.测试结果如下图
4.简单说明:
Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口。
Jedis不是线程安全的,故不应该在多线程环境中共用一个Jedis实例。但是,也应该避免直接创建多个Jedis实例,因为这种做法会导致创建过多的socket连接,性能不高。
要保证线程安全且获得较好的性能,可以使用JedisPool。JedisPool是一个连接池,既可以保证线程安全,又可以保证了较高的效率。
可以声明一个全局的JedisPool变量来保存JedisPool对象的引用,然后在其他地方使用。要知道,JedisPool是一个线程安全的连接池。