数据库查询对象不存储在缓存中(BigMemory Max,Oracle)

时间:2018-01-09 09:50:41

标签: ehcache-bigmemory

我是java世界的新手(spring hibernate等)。我正在为集群环境执行缓存实现的PoC。我只想减少数据库中的命中数,因此希望数据库对象存储在缓存中。我从互联网上了解到,我需要使用JPA结合ehcache和terracotta服务器(BigMemory Max)来使用跨国缓存。 Ehcache可以轻松识别一些跨国管理库,因此我使用了atomikos。如果我走错路,请纠正我。我有以下代码来实现这一点,但我在管理工具中看到了0个缓存对象。

1)ehcache.xml

<cache name="com.example.pocehcache.model.Note"
 maxEntriesLocalHeap="500"
 eternal="false"
 copyOnRead="true"
 copyOnWrite="true"
 transactionalMode="xa">
 <persistence strategy="distributed"/>
 <terracotta consistency="strong"/>
</cache>
<terracottaConfig url="localhost:9510" />

2)Application.properties

# application.properties
spring.cache.ehcache.config=classpath:ehcache.xml
com.atomikos.icatch.threaded_2pc=false
spring.jpa.open-in-view=true


## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=system
spring.datasource.password=password
spring.datasource.driver-class-oracle.jdbc.driver.OracleDriver


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect


# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=create-drop


server.port = 8090

3)控制器类(从互联网上取样)

@RestController
@RequestMapping("/api")
public class NoteController {


 @Autowired
 NoteRepository noteRepository;


 // Get All Notes
 @GetMapping("/notes")
 @Transactional
 public List<Note> getAllNotes() {
 List<Note> notes = noteRepository.findAll();
 return notes;
 }


 // Create a new Note
 @PostMapping("/notes")
 @Transactional
 public Note createNote(@Valid @RequestBody Note note) {
 return noteRepository.save(note);
 }


...

4)申请类

@SpringBootApplication
@EnableJpaAuditing
@EnableAutoConfiguration
@EnableCaching
public class PocEhcacheApplication {
 public static void main(String[] args) {
 SpringApplication.run(PocEhcacheApplication.class, args);
}

5)模型(使用setter,getters,equals和hashCode方法)

    @Entity
    @Table(name = "notes")
    @EntityListeners(AuditingEntityListener.class)
    @JsonIgnoreProperties(value = {"createdAt", "updatedAt"}, 
     allowGetters = true)
    public class Note implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;


     @NotBlank
     private String title;


     @NotBlank
     private String content;


     @Column(nullable = false, updatable = false)
     @Temporal(TemporalType.TIMESTAMP)
     @CreatedDate
     private Date createdAt;


     @Column(nullable = false)
     @Temporal(TemporalType.TIMESTAMP)
     @LastModifiedDate
     private Date updatedAt;
    ...

6)Terracotta管理控制台 o objects in cache in Terracotta management console

任何帮助或指针都将是一个很好的支持。我在类路径中使用BigMemory Max-4.3.4.3.15和相关的jar用于应用程序。如果需要任何其他信息,请告诉我。

注意:如果我将对象直接放入BigMemory,它可以工作(bigMemory.put(new Element(note.getId(),note)))。我能够在兵马俑控制台中看到多少物体。

非常感谢!

0 个答案:

没有答案