jedis操作 redis
spring mvc 配置 jedis連線池
首先 maven 依賴 來一下
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
然後配置檔案搞一下
#redis快取配置
redis.host=127.0.0.1
redis.port=6379
redis.timeout=2000
#注意,如果沒有password,此處不設定值,但這一項要保留
redis.password=xxx
redis.db.index=1
然後spring 配置個bean
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" /> <!-- 最大能夠保持idel狀態的物件數 -->
<property name="maxTotal" value="60000" /> <!-- 最大分配的物件數 -->
<property name="testOnBorrow" value="true" /> <!-- 當呼叫borrow Object方法時,是否進行有效性檢查 -->
</bean>
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="${redis.host}" />
<constructor-arg name="port" value="${redis.port}" type="int" />
<constructor-arg name="timeout" value="${redis.timeout}" type="int" />
<constructor-arg name="password" value="#{'${redis.password}'!=''?'${redis.password}':null}" />
<constructor-arg name="database" value="${redis.db.index}" type="int" />
</bean>
測試類
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml","classpath:mybatis-servlet.xml"})
public class JedisTest {
@Autowired
JedisPool pool;
@Test
public void jedis(){
try {
Jedis jedis=pool.getResource();
Admins admins = new Admins();
admins.setName("張三");
admins.setPwd("lisi");
jedis.set("admin".getBytes(), SerializeUtil.serialize(admins));
admins = (Admins)SerializeUtil.deserialize(jedis.get("admin".getBytes()));
System.err.println(admins.getName());
} catch (Exception e) {
System.err.println(e);
}
}
}
中間用到了一個 幫助類
package com.yc.education.util;
import java.io.*;
public class SerializeUtil {
/**
* 反序列化
* @param bytes
* @return
*/
public static Object deserialize(byte[] bytes) {
Object result = null;
if (isEmpty(bytes)) {
return null;
}
try {
ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
try {
result = objectInputStream.readObject();
}
catch (ClassNotFoundException ex) {
throw new Exception("Failed to deserialize object type", ex);
}
}
catch (Throwable ex) {
throw new Exception("Failed to deserialize", ex);
}
} catch (Exception e) {
}
return result;
}
public static boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
/**
* 序列化
* @param object
* @return
*/
public static byte[] serialize(Object object) {
byte[] result = null;
if (object == null) {
return new byte[0];
}
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
try {
if (!(object instanceof Serializable)) {
throw new IllegalArgumentException(SerializeUtil.class.getSimpleName() + " requires a Serializable payload " +
"but received an object of type [" + object.getClass().getName() + "]");
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();
result = byteStream.toByteArray();
}
catch (Throwable ex) {
throw new Exception("Failed to serialize", ex);
}
} catch (Exception ex) {
}
return result;
}
}
好了 程式碼貼完了
相關文章
- Jedis使用連線池操作redis叢集Redis
- Jedis操作單節點redis,叢集及redisTemplate操作redis叢集(一)Redis
- Jedis操作
- Web - Redis & JedisWebRedis
- Redis(15) jedisRedis
- redis驅動-jedis實現1Redis
- [Redis 客戶端整合] SpringBoot 整合 JedisRedis客戶端Spring Boot
- springboot系列文章之 整合redis 服務 (Lettuce & Jedis)Spring BootRedis
- redis 原始碼分析:Jedis 哨兵模式連線原理Redis原始碼模式
- Redis 客戶端 Jedis、lettuce 和 Redisson 對比Redis客戶端
- [轉載] 使用Redis的Java客戶端JedisRedisJava客戶端
- spring boot使用Jedis整合Redis實現快取(AOP)Spring BootRedis快取
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolRedisclientException
- 深入剖析Redis客戶端Jedis的特性和原理Redis客戶端
- Jedis
- Jedis 連線 Redis報JedisConnectionException: java.net.ConnectException: Connection refusedRedisExceptionJava
- 【進階篇】Redis實戰之Jedis使用技巧詳解Redis
- Jedis bitmap
- Jedis pythonPython
- 操作Redis之go-redisRedisGo
- Golang操作RedisGolangRedis
- python 操作redisPythonRedis
- Go操作RedisGoRedis
- Python操作RedisPythonRedis
- redis基本操作Redis
- 【如何使用jedis】
- Go 操作 Redis 的基本操作GoRedis
- 解決使用jedis連線是報DENIED Redis is running in protected mode錯誤Redis
- 分散式鎖中-基於 Redis 的實現需避坑 - Jedis 篇分散式Redis
- 【Redis】Redis的操作命令(一)——Redis Key命令Redis
- 【Redis】用python操作redis叢集RedisPython
- python操作redis(二)PythonRedis
- Redis管理基本操作Redis
- Redis基礎操作Redis
- Redis有序集合操作Redis
- redis 常用api操作RedisAPI
- 操作Redis之redigoRedisGo
- redis+java操作RedisJava