<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
</dependency>
//設(shè)置ip地址和端口
Jedis jedis = new Jedis("127.0.0.1", 6379);
//保存數(shù)據(jù)
jedis.set("name", "張三");
//獲取數(shù)據(jù)
String value = jedis.get("name");
System.out.println(value);
//釋放資源
jedis.close();
//獲取連接池的配置對象
JedisPoolConfig config = new JedisPoolConfig();
//設(shè)置最大的連接數(shù)
config.setMaxTotal(30);
//設(shè)置最大空閑連接數(shù)
config.setMaxIdle(10);
//獲得連接池
JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
//獲得核心對象
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set("productName", "電腦");
System.out.println(jedis.get("productName"));
System.out.println(jedis.get("name"));
} catch (Exception e) {
e.printStackTrace();
} finally {
//釋放資源
if (null != jedis) {
jedis.close();
}
if (null!=jedisPool){
jedisPool.close();
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者