forked from coderkan/spring-boot-redis-cache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomerServiceImpl.java
106 lines (93 loc) · 3.17 KB
/
CustomerServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.coderkan.services.impl;
import java.util.List;
import java.util.Optional;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.annotation.Observed;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;
import com.coderkan.models.Customer;
import com.coderkan.repositories.CustomerRepository;
import com.coderkan.services.CustomerService;
@Slf4j
@Observed(name="CusterServiceImpl")
@Service
@CacheConfig(cacheNames = "customers")
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private ObservationRegistry observationRegistry;
public String sayHello() {
return Observation
.createNotStarted("CustomerService", observationRegistry)
.observe(this::sayHelloNoObserver);
}
private String sayHelloNoObserver() {
return "Hello World!";
}
@Cacheable
@Override
public List<Customer> getAll() {
waitSomeTime();
return this.customerRepository.findAll();
}
// @CacheEvict(key = "#id", condition = "#id!=null")
// Switching to a CachePut from a CacheEvict
@CachePut(key = "#customer.id")
@Override
public Customer add(Customer customer) {
log.info(" write to database");
return this.customerRepository.save(customer);
}
// this causes all the entries to be deleted if any entries are updated
// @CacheEvict(cacheNames = "customers", allEntries = true)
// this works but is kind of complex. Here customer is the java class object (not customers)
// @CacheEvict(key="#customer?.id", condition="#customer?.id!=null")
// this seems logical, but it doesn't delete the redis cached record
// @CacheEvict(cacheNames = "customers", key = "#id", condition = "#id!=null")
@CachePut(key = "#customer.id")
@Override
public Customer update(Customer customer) {
Optional<Customer> optCustomer = this.customerRepository.findById(customer.getId());
if (!optCustomer.isPresent())
return null;
Customer repCustomer = optCustomer.get();
repCustomer.setName(customer.getName());
repCustomer.setContactName(customer.getContactName());
repCustomer.setAddress(customer.getAddress());
repCustomer.setCity(customer.getCity());
repCustomer.setPostalCode(customer.getPostalCode());
repCustomer.setCountry(customer.getCountry());
return this.customerRepository.save(repCustomer);
}
@CacheEvict(allEntries = true)
@Override
public void evictCache() {
log.info("all entries have been evicted");
}
@Caching(evict = { @CacheEvict(key = "#id", condition = "#id!=null")})
@Override
public void delete(long id) {
if(this.customerRepository.existsById(id)) {
this.customerRepository.deleteById(id);
}
}
@Cacheable(key = "#id", unless = "#result == null")
@Override
public Customer getCustomerById(long id) {
waitSomeTime();
return this.customerRepository.findById(id).orElse(null);
}
private void waitSomeTime() {
log.info("Long Wait Begin");
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Long Wait End");
}
}