Created
January 23, 2019 19:20
-
-
Save gokhanoner/766a1a807744d1a69c6a7799c3f34d73 to your computer and use it in GitHub Desktop.
Spring-Boot with Hazelcast
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
package com.example.democache; | |
import com.hazelcast.config.Config; | |
import com.hazelcast.config.EvictionPolicy; | |
import com.hazelcast.config.MapConfig; | |
import com.hazelcast.config.MaxSizeConfig; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cache.annotation.CacheConfig; | |
import org.springframework.cache.annotation.Cacheable; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.stereotype.Service; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
@SpringBootApplication | |
public class DemoCacheApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(DemoCacheApplication.class, args); | |
} | |
} | |
@Configuration | |
@EnableCaching | |
class CacheConfiguration { | |
private static final String HAZELCAST_LOGGING_TYPE = "hazelcast.logging.type"; | |
@Bean | |
public Config hazelcastConfig() { | |
return new Config().setInstanceName("cache") | |
.addMapConfig( | |
new MapConfig().setName("speciality") | |
.setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE)) | |
.setEvictionPolicy(EvictionPolicy.LRU).setTimeToLiveSeconds(100)); | |
} | |
} | |
interface SpecialityService { | |
List<SpecialityDTO> findAll(); | |
} | |
@Service | |
@CacheConfig(cacheNames = "speciality") | |
@Slf4j | |
class SpecialityServiceImpl implements SpecialityService { | |
@Override | |
@Cacheable | |
public List<SpecialityDTO> findAll() { | |
log.info("inside findAll method"); | |
return IntStream.range(0, 10) | |
.mapToObj(i -> SpecialityDTO.of(i, i + "-name")) | |
.collect(Collectors.toCollection(LinkedList::new)); | |
} | |
} | |
@RestController | |
@AllArgsConstructor | |
class SpecialityResource { | |
private final SpecialityService specialityService; | |
@GetMapping("/specialities") | |
public List<SpecialityDTO> getAllSpecialitys() { | |
return specialityService.findAll(); | |
} | |
} | |
@Data(staticConstructor = "of") | |
class SpecialityDTO { | |
final int id; | |
final String name; | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.1.2.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<groupId>com.example</groupId> | |
<artifactId>demo-cache</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>demo-cache</name> | |
<description>Demo project for Spring Boot</description> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-cache</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.hazelcast</groupId> | |
<artifactId>hazelcast</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.18.4</version> | |
<scope>compile</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment