Redis客户端实现基本数据的操作及使用SpringBoot实现Redis的工具类
Posted double_lifly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis客户端实现基本数据的操作及使用SpringBoot实现Redis的工具类相关的知识,希望对你有一定的参考价值。
Redis客户端实现
Java实现Redis基本数据类型的操作
public class JedisTest {
public static void main(String[] args) {
jedisString();
jedisHash();
jedisList();
jedisSet();
jedisSortedSet();
}
/**
* 一般用法
*/
public static void jedisString(){
Jedis jedis = new Jedis("localhost", 6379);
jedis.auth("123456");
jedis.set("username","lifly");
String username = jedis.get("username");
System.out.println(username);
jedis.close();
}
/**
* jedis操作hash
*/
public static void jedisHash(){
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.auth("123456");
jedis.hset("user","name","lifly");
jedis.hset("user","age","12");
//获取hash
String name = jedis.hget("user", "name");
System.out.println(name);
jedis.close();
}
/**
* jedis操作list
*/
public static void jedisList(){
//创建连接
Jedis jedis = new Jedis("localhost", 6379);
jedis.auth("123456");
//存储数据
jedis.lpush("mylist","a","b","c");
jedis.rpush("mylist","1","2","3");
//获取数据
List<String> mylist = jedis.lrange("mylist", 0, -1);
System.out.println(mylist);
//获取左边单条数据
String lmylist = jedis.lpop("mylist");
System.out.println(lmylist);
//获取右边单条数据
String rmylist = jedis.rpop("mylist");
System.out.println(rmylist);
//关闭连接
jedis.close();
}
/**
* jedis操作set集合
*/
public static void jedisSet(){
Jedis jedis = new Jedis("localhost", 6379);
jedis.auth("123456");
//存储数据
jedis.sadd("myset","java","python","C++");
//获取数据
Set<String> myset = jedis.smembers("myset");
System.out.println(myset);
//关闭连接
jedis.close();
}
/**
* jedis操作sortedset有序集合
*/
public static void jedisSortedSet(){
Jedis jedis = new Jedis("localhost", 6379);
jedis.auth("123456");
//存储数据
jedis.zadd("mysortedset",100,"very nice");
jedis.zadd("mysortedset",90,"very beautiful");
jedis.zadd("mysortedset",80,"very good");
//获取数据
Set<String> mysortedset = jedis.zrange("mysortedset", 0, -1);
System.out.println(mysortedset);
//关闭连接
jedis.close();
}
}
硬编码实现Redis连接池工具类
public class JedisUtil {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisUtil.class.getClassLoader().getResourceAsStream("application.yml");
//创建Properties对象
Properties properties = new Properties();
//加载配置文件
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到jedisPoolConfig中
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.getProperty("max-Total")));
jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.getProperty("max-idle")));
//初始化JedisPool
jedisPool = new JedisPool(jedisPoolConfig, properties.getProperty("host"), Integer.parseInt(properties.getProperty("port")));
}
public static Jedis getJedisPool(){
return jedisPool.getResource();
}
}
测试
public class JedisUtilTest {
public static void main(String[] args) {
Jedis jedis = JedisUtil.getJedisPool();
jedis.auth("123456");
jedis.set("username","lifly");
String username = jedis.get("username");
System.out.println(username);
jedis.close();
}
}
SpringBoot实现Redis连接池
导入坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
配置文件
redis:
pool:
# redis服务器地址(默认为loaclhost)
host: localhost
# redis端口(默认为6379)
port: 6379
# redis访问密码(默认为空)
password: 123456
# redis连接超时时间(单位毫秒)
timeout: 0
# redis连接池配置
config:
# 最大可用连接数(默认为8,负数表示无限)
max-total: 8
# 最大空闲连接数(默认为8,负数表示无限)
max-idle: 8
编写Redis连接池配置类
@SpringBootConfiguration
//@PropertySource(value = {"classpath:application.yml"})
public class RedisConfig {
@Value("${redis.pool.host}")
private String host;
@Value("${redis.pool.port}")
private int port;
@Value("${redis.pool.password}")
private String password;
@Value("${redis.pool.timeout}")
private int timeout;
@Value("${redis.pool.config.max-total}")
private int maxTotal;
@Value("${redis.pool.config.max-idle}")
private int maxIdle;
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setMaxIdle(maxIdle);
return jedisPoolConfig;
}
@Bean
public JedisPool getJedisPool(){
JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
return jedisPool;
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
@Autowired
private JedisPool jedisPool;
@Test
public void test(){
Jedis jedis = jedisPool.getResource();
jedis.set("username","lifly");
String username = jedis.get("username");
System.out.println(username);
jedis.close();
}
}
以上是关于Redis客户端实现基本数据的操作及使用SpringBoot实现Redis的工具类的主要内容,如果未能解决你的问题,请参考以下文章