在Spring Boot Redis缓存中缓存对象时,如何忽略对象的某些字段?

时间:2019-03-28 03:09:36

标签: java spring-boot spring-data-redis

当spring boot redis缓存一个对象时,我想忽略由@MayToOne和@OneToMany注释的字段

为什么我要忽略它们?因为在我的用例中,我需要忽略它们,而这是一个长话不说了。

这里是对象。

@Entity
@Table(name = "tga_project_application_record")
@Data
@EntityListeners(AuditingEntityListener.class)
public class ProjectApplicationRecord implements Serializable {
    private static final Long UID = 1L;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;


    @JsonIgnore
    @ManyToOne
    @JoinColumn(nullable = false)
    private User user;


    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false)
    private Project project;


    private String processInstanceId;


    @Type(type = "json")
    @Column(columnDefinition = "json", nullable = false)
    private Object applicationInfo;


    @Column(nullable = false)
    private ApplicationStatus status;


    @Enumerated(EnumType.STRING)
    private RealTimeStatus realTimeStatus;


    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dynamic_form_id", nullable = false)
    private DynamicForm form;



    @Type(type = "json")
    @Column(columnDefinition = "json", nullable = false)
    private Set<ApplicationFile> applicationFiles;


    @CreatedDate
    private Timestamp createdAt;


    @LastModifiedDate
    private Timestamp updatedAt;
}

我正在使用Spring Boot缓存的注释来管理缓存。 这是我如何使用这些注释来管理缓存的部分。

@Caching(
            put = {@CachePut(value = "projectApplicationRecordCache", key = "#id")},
            evict = {
                    @CacheEvict(value = {"projectApplicationRecordPaginatedCache", "projectApplicationRecordListCache", "projectApplicationRecordCacheByProcessInstanceId"}, allEntries = true),
                    @CacheEvict(value = {"getByProjectIdAndUserIdIfThatRecordIsNormalCache"}, key = "{#result.project.id, #result.user.id}"),
            }
    )
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public ProjectApplicationRecord patchUpdate(Long id, ProjectApplicationRecord object) {
        apiUtilsService.notNull(object);
        noViolationUniqueConstraint(object);
        ProjectApplicationRecord fullRecord = getByIdForUpdate(id);
        apiUtilsService.merge(fullRecord, getSafeObj(fullRecord, object));
        return recordRepository.save(fullRecord);
    }


    @Caching(
            put = {@CachePut(value = "projectApplicationRecordCache", key = "#result.id")},
            evict = {
                    @CacheEvict(value = {"projectApplicationRecordPaginatedCache", "projectApplicationRecordListCache"}, allEntries = true),
                    @CacheEvict(value = "getByProjectIdAndUserIdIfThatRecordIsNormalCache", key = "{#result.project.id, #result.user.id}")
            }
    )
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public ProjectApplicationRecord create(ProjectApplicationRecord object) {
        apiUtilsService.notNull(object);
        noViolationUniqueConstraint(object);
        return recordRepository.save(getSafeObj(null, object));
    }

谢谢大家,我的英语不好,请原谅我。

也许我可以使用自定义序列化程序替换spring-boot-data-redis的默认序列化程序以获得支持,而忽略@MayToOne和@OneToMany注释的字段?

0 个答案:

没有答案