-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 18 additions & 2 deletions
20
src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
package com.ghostchu.btn.sparkle.module.analyse.impl; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
|
||
@Entity | ||
@Table(name = "analyse_rules", | ||
indexes = {@Index(columnList = "module"), @Index(columnList = "ip")} | ||
) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Data | ||
@Getter | ||
@Setter | ||
@DynamicUpdate | ||
public class AnalysedRule { | ||
@Id | ||
@GeneratedValue | ||
@Column(nullable = false, unique = true) | ||
private Long id; | ||
@Column(nullable = false) | ||
private String ip; | ||
@Column(nullable = false) | ||
private String module; | ||
@Column(nullable = false) | ||
private String comment; | ||
} |
38 changes: 9 additions & 29 deletions
38
src/main/java/com/ghostchu/btn/sparkle/module/analyse/impl/AnalysedRuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,21 @@ | ||
package com.ghostchu.btn.sparkle.module.analyse.impl; | ||
|
||
import lombok.Cleanup; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ScanOptions; | ||
import org.springframework.stereotype.Service; | ||
import com.ghostchu.btn.sparkle.module.repository.SparkleCommonRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Service | ||
public class AnalysedRuleRepository { | ||
@Repository | ||
public interface AnalysedRuleRepository extends SparkleCommonRepository<AnalysedRule, Long> { | ||
//List<AnalysedRule> findByModule(String module); | ||
@Autowired | ||
private RedisTemplate<String, AnalysedRule> redisTemplate; | ||
List<AnalysedRule> findByModuleOrderByIpAsc(String module); | ||
|
||
public void deleteAllByModule(String module) { | ||
redisTemplate.unlink("analysed_rules:" + module); | ||
} | ||
|
||
public Set<AnalysedRule> findAll() { | ||
Set<AnalysedRule> analysedRules = new HashSet<>(); | ||
@Cleanup | ||
var it = redisTemplate.opsForSet().scan("analysed_rules:*", ScanOptions.scanOptions().build()); | ||
while (it.hasNext()) { | ||
analysedRules.add(it.next()); | ||
} | ||
return analysedRules; | ||
} | ||
long deleteAllByModule(String module); | ||
|
||
public Set<AnalysedRule> findAllByModule(String module) { | ||
return redisTemplate.opsForSet().members("analysed_rules:" + module); | ||
} | ||
List<AnalysedRule> findAll(); | ||
|
||
public void replaceAll(String module, List<AnalysedRule> rules) { | ||
default void replaceAll(String module, List<AnalysedRule> rules) { | ||
deleteAllByModule(module); | ||
redisTemplate.opsForSet().add("analysed_rules:" + module, rules.toArray(new AnalysedRule[0])); | ||
saveAll(rules); | ||
} | ||
} |