Spring-data-redisによるライブラリ選択操作
4127 ワード
前言
コンフィギュレーション単純な構成のためにspringbootに基づいてreids操作 を構成する.
springboot構成に基づいてspring-data-redisを導入し、異なるバージョンのspringbootが導入したspring-boot-starter-data-redisは異なる場合があります.具体的には公式ドキュメントを参照してください.
アプリケーションでymlまたはアプリケーション.propertiesで構成されている基本的な接続情報は次のとおりです.
その後カスタムRedisTemplate spring-data-redisのStringRedisTemplateを継承し、REDIS_を定義します.DB_INDEXはThreadLocalタイプで、各スレッドのライブラリ選択操作は互いに影響しません.
カスタムRedisTemplateをBeanコンテナに登録
上記のコードはFastJsonシートredisのデフォルトシーケンス化ツールを使用しており、FastJsonシーケンス化を構成するにはカスタムシーケンス化器が必要です.
その後、ライブラリ選択操作を実行できます.
もちろん、上記の操作をRedisServicesパッケージに自分で書いたほうがいいです.
注意:
Redisがライブラリ選択操作を行うと実行効率は向上しませんが、トラフィック量が大きすぎる場合は、異なるモジュールの異なるトラフィックのreidsを異なるライブラリに格納し、データを効率的に区別できます.
もちろんredis keyを設計する際にできる業務区分を行うことを強くお勧めします.後期メンテナンスに便利です.
spring-data-redis reids , , spring-data-redis redis ,
コンフィギュレーション
springboot構成に基づいてspring-data-redisを導入し、異なるバージョンのspringbootが導入したspring-boot-starter-data-redisは異なる場合があります.具体的には公式ドキュメントを参照してください.
org.springframework.boot
spring-boot-starter-data-redis
1.5.9.RELEASE
アプリケーションでymlまたはアプリケーション.propertiesで構成されている基本的な接続情報は次のとおりです.
spring:
redis:
database: 0
host: 127.0.0.1
password:
pool:
max-active: 8
max-wait: 5000
max-idle: 8
min-idle: 3
timeout: 5000
その後カスタムRedisTemplate spring-data-redisのStringRedisTemplateを継承し、REDIS_を定義します.DB_INDEXはThreadLocalタイプで、各スレッドのライブラリ選択操作は互いに影響しません.
public static ThreadLocal REDIS_DB_INDEX = new ThreadLocal(){
@Override
protected Integer initialValue() {
return 0;
}
};
@Override
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
try {
Integer dbIndex = REDIS_DB_INDEX.get();
// dbIndex
if (dbIndex != null) {
if (connection instanceof JedisConnection) {
if (((JedisConnection) connection).getNativeConnection().getDB().intValue() != dbIndex) {
connection.select(dbIndex);
}
} else {
connection.select(dbIndex);
}
} else {
connection.select(0);
}
} finally {
REDIS_DB_INDEX.remove();
}
return super.preProcessConnection(connection, existingConnection);
}
}
カスタムRedisTemplateをBeanコンテナに登録
@Configuration
public class RedisConfig {
@Bean
public RedisSerializer fastJson2JsonRedisSerializer() {
return new FastJson2JsonRedisSerializer
上記のコードはFastJsonシートredisのデフォルトシーケンス化ツールを使用しており、FastJsonシーケンス化を構成するにはカスタムシーケンス化器が必要です.
public class FastJson2JsonRedisSerializer implements RedisSerializer {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class clazz;
public FastJson2JsonRedisSerializer(Class clazz) {
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return (T) JSON.parseObject(str, clazz);
}
}
その後、ライブラリ選択操作を実行できます.
RedisTemplate.REDIS_DB_INDEX.set(10);
redisTemplate.opsForValue().set("author","stevejobson");
もちろん、上記の操作をRedisServicesパッケージに自分で書いたほうがいいです.
注意:
Redisがライブラリ選択操作を行うと実行効率は向上しませんが、トラフィック量が大きすぎる場合は、異なるモジュールの異なるトラフィックのreidsを異なるライブラリに格納し、データを効率的に区別できます.
もちろんredis keyを設計する際にできる業務区分を行うことを強くお勧めします.後期メンテナンスに便利です.