Redisを使用してSpringBootプロジェクトにセッションを保存する方法
1. Redisとは何ですか?
Redisはキーバリューストレージシステムです。Memcachedと同様に、string(string)、list(linked list)、set(collection)、zset(sorted set-ordered set)、hash(hash type)など、ストレージに対して比較的多くの値タイプをサポートします。これらのデータタイプはすべて、プッシュ/ポップ、追加/削除、交差、結合と差分、およびより豊富な操作をサポートし、これらの操作はすべてアトミックです。これに基づいて、redisはさまざまなソート方法をサポートしています。memcachedと同様に、効率を確保するために、データはメモリにキャッシュされます。違いは、redisが定期的に更新されたデータをディスクに書き込んだり、変更操作を追加のレコードファイルに書き込んだりすることです。これに基づいて、マスタースレーブ(マスタースレーブ)同期を実現します。
2.この場合、Redisは何をしますか?
WEB開発では、サーバーは各ユーザーのブラウザー用のセッションオブジェクト(セッションオブジェクト)を作成でき、ブラウザーはセッションオブジェクトを排他的に所有します(デフォルト)。サーバーは、検証のためにセッションに保存されている情報を取得します。
実際のアプリケーションシナリオにより、接続数は膨大であり、複数のサーバーが必要です。負荷分散を実現するために、同じサーバーに毎回アクセスすることはできません。セッションストレージを統合するには、セッションの管理を支援するRedisが必要です。
3.AlibabaクラウドサーバーにRedisをインストールします
ここで、RediscentOsをインストールして構成するために他の人が書いたチュートリアルを見つけました
第4に、SpringBootプロジェクトにyml構成を追加します
spring:
cache:
type: REDIS
redis:
host: 你的ip
port: 你的端口
password: 你的密码
database: 0
5、構成クラスを作成します
新しいRedisConfigクラスを作成します。クラスの内容は次のとおりです。ここでは、デフォルトの有効期限999999秒を構成し、redisキーを「cacheName:xxxx」形式に設定し、値のシリアル化をjson形式に変更します。
これで、プロジェクトでredisを楽しく使用できます。Springbootは、キャッシュの操作をカプセル化するのに役立ちます。注釈を直接使用して便利に使用できます。
package com.kid.config;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.CacheKeyPrefix;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.time.Duration;
/**
* @ClassName RedisConfig
* @Description TODO
* @Autor T_Antry
* @Date 2020/10/21 13:10
* @Version 1.0
*/
@Configuration
public class RedisConfig {
/**
* 默认超时时间
*/
private static final long DEFAULT_TTL = 999999L;
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//自定义key前缀
CacheKeyPrefix cacheKeyPrefix = new CacheKeyPrefix() {
@Override
public String compute(String cacheName) {
return cacheName+":";
}
};
//设置CacheManager的值序列化方式为json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
//生成RedisCacheConfiguration配置
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.computePrefixWith(cacheKeyPrefix)
.serializeValuesWith(pair);
//设置默认超过期时间是30秒
defaultCacheConfig.entryTtl(Duration.ofSeconds(DEFAULT_TTL));
//初始化RedisCacheManager
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
}
@EnableCachingアノテーションをスタートアップクラスに追加します。このアノテーションが追加されていない場合、キャッシュは有効になりません。
package com.kid;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
// 作用:扫描该包下的所有java类 (扫描器标签)
// 作用:自动加载配置类,我们springboot的配置很多都写成类的形式(fsatjson)
// 作用:到时候打包的时候识别成springboot框架
@SpringBootApplication
@MapperScan("com.kid.mapper")
@ServletComponentScan
@EnableCaching
public class Boot {
public static void main(String[] args) {
SpringApplication.run(Boot.class, args);
}
}