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;
}
}
好了 程式碼貼完了
相關文章
- java操作redis。jedis使用apiJavaRedisAPI
- Jedis使用連線池操作redis叢集Redis
- Spring Boot 中使用 Jedis 來操作 RedisSpring BootRedis
- Jedis操作單節點redis,叢集及redisTemplate操作redis叢集(一)Redis
- Jedis操作
- Web - Redis & JedisWebRedis
- Redis(15) jedisRedis
- redis(二)redis概述與jedis的使用Redis
- redis驅動-jedis實現1Redis
- Redis筆記2:Jedis連線池Redis筆記
- [轉載] 使用Redis的Java客戶端JedisRedisJava客戶端
- redis 原始碼分析:Jedis 哨兵模式連線原理Redis原始碼模式
- Redis 客戶端 Jedis、lettuce 和 Redisson 對比Redis客戶端
- Redis學習記錄(二)--使用Jedis連線Redis
- 深入剖析Redis客戶端Jedis的特性和原理Redis客戶端
- Redis筆記3:Jedis連線自動釋放Redis筆記
- spring boot使用Jedis整合Redis實現快取(AOP)Spring BootRedis快取
- 【進階篇】Redis實戰之Jedis使用技巧詳解Redis
- Jedis bitmap
- springboot系列文章之 整合redis 服務 (Lettuce & Jedis)Spring BootRedis
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolRedisclientException
- Jedis pythonPython
- Jedis實戰
- 分散式鎖中-基於 Redis 的實現需避坑 - Jedis 篇分散式Redis
- 操作Redis之go-redisRedisGo
- Go操作RedisGoRedis
- Golang操作RedisGolangRedis
- Redis基本操作Redis
- redis操作類Redis
- Go 操作 Redis 的基本操作GoRedis
- 【Redis】Redis的操作命令(一)——Redis Key命令Redis
- 解決使用jedis連線是報DENIED Redis is running in protected mode錯誤Redis
- Python操作RedisPythonRedis
- Redis事務操作Redis
- python 操作redisPythonRedis
- Redis管理基本操作Redis
- redis 常用api操作RedisAPI
- Laravel Redis操作大全LaravelRedis