java—如何将hibernate缓存与外部更改同步

wgxvkvu9  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(272)
@Entity
@Table(name = "abc")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Abc{

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

    @OneToMany(mappedBy = "Abc")
    @Column(name = "name", nullable = false)
    private String name;

    @Cache
    private List<AbcAccess> accesses = new ArrayList<>();
}
@Entity
@Table(name = "abc_access")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AbcAccess {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")

    private Long id;

    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne(optional = false)
    private Abc abc;
}

背景:springboot+springjpa(hibernate)+ehcache,二级缓存启用。 Cacheorg.hibernate.annotations.Cache 配置(用kotlin编写):

@Configuration
@EnableCaching
class AbcConifg{

    private val jcacheConfiguration: javax.cache.configuration.Configuration<Any, Any>

    init {
        val ehcache = jHipsterProperties.cache.ehcache

        jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
            CacheConfigurationBuilder.newCacheConfigurationBuilder(
                Any::class.java, Any::class.java,
                ResourcePoolsBuilder.heap(ehcache.maxEntries)
            )
                .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(ehcache.timeToLiveSeconds.toLong())))
                .build()
        )
    }

    @Bean
    fun hibernatePropertiesCustomizer(cacheManager: javax.cache.CacheManager) = HibernatePropertiesCustomizer {
        hibernateProperties -> hibernateProperties[ConfigSettings.CACHE_MANAGER] = cacheManager
    }
}

当我询问 Abc 从数据库中,它将结果缓存到二级缓存中。
现在我再插入一个 AbcAccess 进入数据库,然后我查询 Abc 再次从数据库中,它将只从缓存中获取结果。以及 accessAbc 不是实时的,它错过了一个 access .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题