|
| 1 | +package com.mvpjava; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.cache.annotation.CacheConfig; |
| 7 | +import org.springframework.cache.annotation.CacheEvict; |
| 8 | +import org.springframework.cache.annotation.CachePut; |
| 9 | +import org.springframework.cache.annotation.Cacheable; |
| 10 | +import org.springframework.cache.annotation.Caching; |
| 11 | +import org.springframework.dao.DuplicateKeyException; |
| 12 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 13 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 14 | +import org.springframework.data.mongodb.core.query.Query; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +@Component |
| 18 | +@CacheConfig(cacheNames = {"AIRCRAFTS"}) |
| 19 | +public class AircraftService { |
| 20 | + |
| 21 | + private static final Logger logger = LoggerFactory.getLogger(AircraftService.class); |
| 22 | + private final MongoTemplate template; |
| 23 | + private final String mongoCollection = "aircraft"; |
| 24 | + |
| 25 | + @Autowired |
| 26 | + public AircraftService(MongoTemplate template) { |
| 27 | + this.template = template; |
| 28 | + } |
| 29 | + |
| 30 | + @Cacheable(unless = "#result == null") |
| 31 | + public Aircraft getAircraftByModelName(String modelName) { |
| 32 | + logger.info("Executing getAircraftByModelName for model:{} \tCache MISS!", modelName); |
| 33 | + slowDownForDemo(); |
| 34 | + Query query = new Query(Criteria.where("model").is(modelName)); |
| 35 | + return template.findOne(query, Aircraft.class, mongoCollection); |
| 36 | + } |
| 37 | + |
| 38 | + @CachePut(key = "#aircraft.model", condition = "#aircraft.topSpeed > 0", unless = "#result == null") |
| 39 | + public Aircraft createAircraft(Aircraft aircraft) { |
| 40 | + logger.info("Executing createAircraft, model:{}", aircraft.getModel()); |
| 41 | + try { |
| 42 | + template.insert(aircraft, mongoCollection); |
| 43 | + } catch (DuplicateKeyException dke) { |
| 44 | + //OK for demo |
| 45 | + } |
| 46 | + return template.findOne( |
| 47 | + new Query(Criteria.where("model").is(aircraft.getModel())), |
| 48 | + Aircraft.class, mongoCollection);//has id |
| 49 | + } |
| 50 | + |
| 51 | + @CacheEvict(key = "#aircraft.model") |
| 52 | + public void updateAircraft(Aircraft aircraft) { |
| 53 | + logger.info("Executing updateAircraft, model:{} topSpeed: {} Cache Evict!", |
| 54 | + aircraft.getModel(), aircraft.getTopSpeed()); |
| 55 | + try { |
| 56 | + template.save(aircraft, mongoCollection); |
| 57 | + } catch (DuplicateKeyException dke) { |
| 58 | + //OK for demo |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @CacheEvict(key = "#aircraft.model") |
| 63 | + public void removeAircraft(Aircraft aircraft) { |
| 64 | + logger.info("Executing removeAircraft, model:{} \tCache Evict!", aircraft.getModel()); |
| 65 | + template.remove(aircraft, mongoCollection); |
| 66 | + } |
| 67 | + |
| 68 | + @Caching(evict = { |
| 69 | + @CacheEvict(value = "AIRCRAFTS", allEntries = true), |
| 70 | + @CacheEvict(value = "SECOND_CACHE", allEntries = true) |
| 71 | + }) |
| 72 | + public void clearAllCaches() { |
| 73 | + logger.info("Cleared all caches"); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private void slowDownForDemo() { |
| 78 | + try { |
| 79 | + Thread.sleep(2000L); |
| 80 | + } catch (Exception e) { |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments