使用Redisの
1.概要ばねブートスタータデータRedisの依存
2.プロジェクトファイルの設定
最初のスタートのRedisを使用する前に
#redis設定 #redisサーバーのアドレス spring.redis.host = localhostを #redisサーバのポート spring.redis.port = 6379 #データベース構成 spring.datasource.driver - クラス -name = com.mysql.cj.jdbc.Driver spring.datasourceする.url = JDBC:MySQLの:// localhostの:? 3306 / characterEncodingテスト= UTF-8&useSslオプション=偽&serverTimezone%2B8 = GMT =&allowPublicKeyRetrieval trueに spring.datasource.username = ルート spring.datasource.password = 123456abc 検証の負荷が休止状態時に##、検証データベーステーブル構造の作成 各負荷休止を作成##を、データ損失のデータベーステーブルの原因となるデータベースのテーブル構造を再作成します。 ##作成 -あなたは、負荷の休止状態の低下を作成する場合は、削除テーブル構造終了 ##更新の負荷が自動的に休止状態のデータベース構造を更新する ##何も起動しないときは何もしません spring.jpa.hibernate.ddl -auto = 作成 ##コンソール印刷のSQL spring.jpaを。表示 -sql =をtrueに
3.セットとメソッドのRedisのを取得
@Service パブリック クラスRedisService { @Resource プライベート RedisTemplate <文字列、オブジェクト> redisTemplate。 // 或者这时不能有<文字列、オブジェクト>、这个特殊 // @Autowired // プライベートRedisTemplate redisTemplate。 公共 ボイド集合(文字列キー、オブジェクト値){ redisTemplate.setKeySerializer(新しいStringRedisSerializer())。 redisTemplate.setValueSerializer(新しい Jackson2JsonRedisSerializer(オブジェクト。クラス)); ValueOperations <文字列、オブジェクト> VO = redisTemplate.opsForValue()。 vo.set(キー、値)。 } 公共 無効セット(文字列のキー、オブジェクトの値が、長い時間、TimeUnitでトン){ redisTemplate.setKeySerializer(新StringRedisSerializer()); redisTemplate.setValueSerializer(新しい Jackson2JsonRedisSerializer(オブジェクト。クラス)); ValueOperations <文字列、オブジェクト> VO = redisTemplate.opsForValue()。 vo.set(キー、値)。 } パブリックオブジェクトGET(文字列キー){ ValueOperationsの<string、オブジェクト> VO = redisTemplate.opsForValue()。 リターンvo.get(キー); } }
4. Userエンティティクラスと書き込みリポジトリ
5.Controllerテスト
@RestController パブリック クラスUserControllerで{ @Autowired プライベートRedisService redisServiceを。 @Autowired プライベートUserRepository userRepository。 @GetMapping(値 = "saveUser" ) パブリック文字列saveUser(ロングID、文字列ユーザ名、文字列のuserPassword){ ユーザーユーザ = 新しいユーザー(ID、ユーザ名、のuserPassword)。 redisService.set(id.toString()、ユーザ)。 戻る「成功」。 } @GetMapping(値 = "getUserById" ) パブリックオブジェクトgetUserById(ロングID){ リターンredisService.get(id.toString()); } (@GetMapping "/ saveUser2" ) パブリックユーザsaveUser2(ロングID、文字列ユーザ名、文字列のuserPassword){ ユーザーユーザ = 新しいユーザー(ID、ユーザ名、のuserPassword)。 userRepository.save(ユーザ)。 リターンユーザー; } @GetMapping(値 = "のgetUser" ) パブリックオブジェクトのgetUser(ロングID){ Objectオブジェクト = redisService.get(id.toString())。 もし(オブジェクト== NULL ){ 物体 =。(userRepository.findById(ID))を取得(); もし(オブジェクト!= NULL ){ redisService.set(id.toString()、オブジェクト、 100L 、TimeUnit.SECONDS)。 } } 戻りオブジェクト。 } }
キーと値に注意をシリアル化する必要があります。
使用Redisのキャッシュ
ただ、データベースの使用、および実環境をRedisのちょうどその時、一般的に、従来のデータベースに基づく、Redisのデータベースによって補完。通常、Redisのキャッシュの役割を果たしています。
新しいクラスRedisService 1.は、メソッドの有効期限を設定します。
公共 無効セット(文字列のキー、オブジェクトの値が、長い時間、TimeUnitでトン){ redisTemplate.setKeySerializer(新StringRedisSerializer()); redisTemplate.setValueSerializer(新しい Jackson2JsonRedisSerializer(オブジェクト。クラス)); ValueOperations <文字列、オブジェクト> VO = redisTemplate.opsForValue()。 vo.set(キー、値)。 }
コントローラクラスの2つの新しい方法
( "/ saveUser2" @GetMapping ) パブリックユーザsaveUser2(ロングID、文字列ユーザ名、文字列のuserPassword){ ユーザーユーザ = 新しいユーザー(ID、ユーザ名、のuserPassword)。 userRepository.save(ユーザ)。 リターンユーザー; } @GetMapping(値 = "のgetUser" ) パブリックオブジェクトのgetUser(ロングID){ Objectオブジェクト = redisService.get(id.toString())。 もし(オブジェクト== NULL ){ 物体 = (userRepository.findById(ID))(GET)。 もし(オブジェクト!= nullを){ redisService.set(id.toString()、オブジェクト、 100L 、TimeUnit.SECONDS)。 } } 戻りオブジェクト。 }