petclinic-aplcache

added aplcache cache

6/29/2019 6:37:39 AM

Details

docker-compose.yml 10(+4 -6)

diff --git a/docker-compose.yml b/docker-compose.yml
index cdcd084..8d58c76 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -10,12 +10,10 @@ services:
     command: bash -c 'while !</dev/tcp/database/3306; do sleep 5; done; bash run.sh'
     environment:
       - JAVA_OPTS=${JAVA_OPTS:-"-Xms4096m -Xmx6124m"}
-      - TRACER_ENABLE=${TRACER_ENABLE:-true}
-      - TRACER_MINIMUM_EXECUTION_TIME=${TRACER_MINIMUM_EXECUTION_TIME:-1}
+      - CACHE_EVENTS=${CACHE_EVENTS:-/caching-approaches-comparison/applications/output/petclinic-aplcache-cache}
+      - CACHE_REGISTER_SIZE=false
+      - APLCACHE_CACHEABLE_PARAMETERS=/caching-approaches-comparison/applications/output/aplcache-petclinic-parameters.json
       - TRACER_SERIALISE_INTERNALS=false
-      - TRACER_VERBOSE=true
-      - TRACER_TRACES=/caching-approaches-comparison/applications/traces/petclinic
-      - TRACER_LOG=/caching-approaches-comparison/applications/output/petclinic-tracer.log
     volumes:
       - application:/application
       - /root/.m2:/root/.m2
@@ -47,4 +45,4 @@ services:
 
 volumes:
   application:
-  database:
\ No newline at end of file
+  database:

pom.xml 57(+8 -49)

diff --git a/pom.xml b/pom.xml
index ca29161..5cacb1a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -117,8 +117,14 @@
     </dependency>
 
         <dependency>
-            <groupId>br.ufrgs.inf.prosoft.applicationtracer</groupId>
-            <artifactId>ApplicationTracer</artifactId>
+            <groupId>br.ufrgs.inf.prosoft.cache</groupId>
+            <artifactId>Cache</artifactId>
+            <version>1.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>br.ufrgs.inf.prosoft.aplcache</groupId>
+            <artifactId>APLCache</artifactId>
             <version>1.0</version>
         </dependency>
 
@@ -126,53 +132,6 @@
 
   <build>
     <plugins>
-
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>aspectj-maven-plugin</artifactId>
-                <version>1.11</version>
-                <configuration>
-                    <showWeaveInfo>false</showWeaveInfo>
-                    <complianceLevel>1.8</complianceLevel>
-                    <source>1.6</source>
-                    <target>1.6</target>
-                    <Xlint>ignore</Xlint>
-                    <encoding>UTF-8</encoding>
-                    <verbose>false</verbose>
-                    <forceAjcCompile>true</forceAjcCompile>
-                    <sources/>
-                    <weaveDirectories>
-                        <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
-                    </weaveDirectories>
-                    <aspectLibraries>
-                        <aspectLibrary>
-                            <groupId>br.ufrgs.inf.prosoft.applicationtracer</groupId>
-                            <artifactId>ApplicationTracer</artifactId>
-                        </aspectLibrary>
-                    </aspectLibraries>
-                </configuration>
-                <executions>
-                    <execution>
-                        <phase>process-classes</phase>
-                        <goals>
-                            <goal>compile</goal>
-                        </goals>
-                    </execution>
-                </executions>
-                <dependencies>
-                    <dependency>
-                        <groupId>org.aspectj</groupId>
-                        <artifactId>aspectjrt</artifactId>
-                        <version>1.9.1</version>
-                    </dependency>
-                    <dependency>
-                        <groupId>org.aspectj</groupId>
-                        <artifactId>aspectjtools</artifactId>
-                        <version>1.9.1</version>
-                    </dependency>
-                </dependencies>
-            </plugin>
-
       <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
diff --git a/src/main/java/org/petclinic/owner/OwnerController.java b/src/main/java/org/petclinic/owner/OwnerController.java
index 1b61d00..392c23e 100644
--- a/src/main/java/org/petclinic/owner/OwnerController.java
+++ b/src/main/java/org/petclinic/owner/OwnerController.java
@@ -26,6 +26,8 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.servlet.ModelAndView;
 
+import br.ufrgs.inf.prosoft.aplcache.caching.APLCache;
+
 import javax.validation.Valid;
 import java.util.Collection;
 import java.util.Map;
@@ -119,6 +121,8 @@ class OwnerController {
         }
     }
 
+    public static APLCache<ModelAndView> showOwnerCache = new APLCache<>("showOwnerCache");
+
     /**
      * Custom handler for displaying an owner.
      *
@@ -127,9 +131,11 @@ class OwnerController {
      */
     @GetMapping("/owners/{ownerId}")
     public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
-        ModelAndView mav = new ModelAndView("owners/ownerDetails");
-        mav.addObject(this.owners.findById(ownerId));
-        return mav;
+        return showOwnerCache.computeIfAbsent(Thread.currentThread(), new Object[]{ownerId}, () -> {
+            ModelAndView mav = new ModelAndView("owners/ownerDetails");
+            mav.addObject(this.owners.findById(ownerId));
+            return mav;
+        }, 60000);
     }
 
 }
diff --git a/src/main/java/org/petclinic/owner/PetController.java b/src/main/java/org/petclinic/owner/PetController.java
index 2d49778..6fbc55d 100644
--- a/src/main/java/org/petclinic/owner/PetController.java
+++ b/src/main/java/org/petclinic/owner/PetController.java
@@ -23,6 +23,9 @@ import org.springframework.validation.BindingResult;
 import org.springframework.web.bind.WebDataBinder;
 import org.springframework.web.bind.annotation.*;
 
+import br.ufrgs.inf.prosoft.aplcache.caching.APLCache;
+import br.ufrgs.inf.prosoft.cache.GetterCache;
+
 import javax.validation.Valid;
 import java.util.Collection;
 
@@ -45,14 +48,22 @@ class PetController {
         this.owners = owners;
     }
 
+    public static GetterCache<Collection<PetType>> populatePetTypesCache = new GetterCache<>("populatePetTypesCache");
+
     @ModelAttribute("types")
     public Collection<PetType> populatePetTypes() {
-        return this.pets.findPetTypes();
+        return populatePetTypesCache.computeIfAbsent(() -> {
+            return this.pets.findPetTypes();
+        }, 60000);
     }
 
+    public static APLCache<Owner> findOwnerCache = new APLCache<>("findOwnerCache");
+
     @ModelAttribute("owner")
     public Owner findOwner(@PathVariable("ownerId") int ownerId) {
-        return this.owners.findById(ownerId);
+        return findOwnerCache.computeIfAbsent(Thread.currentThread(), new Object[]{ownerId}, () -> {
+            return this.owners.findById(ownerId);
+        }, 60000);
     }
 
     @InitBinder("owner")
diff --git a/src/main/java/org/petclinic/owner/PetTypeFormatter.java b/src/main/java/org/petclinic/owner/PetTypeFormatter.java
index ec0dbdb..59f759f 100644
--- a/src/main/java/org/petclinic/owner/PetTypeFormatter.java
+++ b/src/main/java/org/petclinic/owner/PetTypeFormatter.java
@@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.format.Formatter;
 import org.springframework.stereotype.Component;
 
+import br.ufrgs.inf.prosoft.aplcache.caching.APLCache;
+
 /**
  * Instructs Spring MVC on how to parse and print elements of type 'PetType'. Starting from Spring 3.0, Formatters have
  * come as an improvement in comparison to legacy PropertyEditors. See the following links for more details: - The
@@ -51,15 +53,22 @@ public class PetTypeFormatter implements Formatter<PetType> {
         return petType.getName();
     }
 
+    public static APLCache<PetType> parseCache = new APLCache<>("parseCache");
     @Override
     public PetType parse(String text, Locale locale) throws ParseException {
-        Collection<PetType> findPetTypes = this.pets.findPetTypes();
-        for (PetType type : findPetTypes) {
-            if (type.getName().equals(text)) {
-                return type;
+        PetType petType = parseCache.computeIfAbsent(Thread.currentThread(), new Object[]{text, locale}, () -> {
+            Collection<PetType> findPetTypes = this.pets.findPetTypes();
+            for (PetType type : findPetTypes) {
+                if (type.getName().equals(text)) {
+                    return type;
+                }
             }
+            return null;
+        }, 60000);
+        if (petType == null) {
+            throw new ParseException("type not found: " + text, 0);
         }
-        throw new ParseException("type not found: " + text, 0);
+        return petType;
     }
 
 }
diff --git a/src/main/java/org/petclinic/vet/VetController.java b/src/main/java/org/petclinic/vet/VetController.java
index dd248cf..151e7ed 100644
--- a/src/main/java/org/petclinic/vet/VetController.java
+++ b/src/main/java/org/petclinic/vet/VetController.java
@@ -15,6 +15,8 @@
  */
 package org.petclinic.vet;
 
+import br.ufrgs.inf.prosoft.cache.GetterCache;
+import java.util.Collection;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -43,18 +45,27 @@ class VetController {
         // Here we are returning an object of type 'Vets' rather than a collection of Vet
         // objects so it is simpler for Object-Xml mapping
         Vets vets = new Vets();
-        vets.getVetList().addAll(this.vets.findAll());
+        vets.getVetList().addAll(findAll());
         model.put("vets", vets);
         return "vets/vetList";
     }
 
-    @GetMapping({ "/vets.json", "/vets.xml" })
-    public @ResponseBody Vets showResourcesVetList() {
+    public static GetterCache<Vets> showResourcesVetListCache = new GetterCache<>("showResourcesVetListCache");
+
+    @GetMapping({"/vets.json", "/vets.xml"})
+    public @ResponseBody
+    Vets showResourcesVetList() {
         // Here we are returning an object of type 'Vets' rather than a collection of Vet
         // objects so it is simpler for JSon/Object mapping
-        Vets vets = new Vets();
-        vets.getVetList().addAll(this.vets.findAll());
-        return vets;
+        return showResourcesVetListCache.computeIfAbsent(() -> {
+            Vets vets = new Vets();
+            vets.getVetList().addAll(findAll());
+            return vets;
+        });
+    }
+
+    private Collection<Vet> findAll() {
+        return this.vets.findAll();
     }
 
 }