1

我在 maven 项目中实现缓存系统。我没有使用 spring 或任何框架。概念是:

 1.At first user will hit the API and Check whether the data is available in cache or not.
 2.If cache is empty ,call the database and put values in cache and send them back.
 3.If cache is not empty, then return the values from cache.

因此,为了进行适当的组织,我使用了 Java 缓存系统(JCS)库,我参考了该库:

https://www.javaworld.com/article/2077936/open-source-java-projects-java-caching-system.html

所以,我需要有cache.ccf文件,该文件将包含缓存内存的所有配置。

我在我的服务类中使用的逻辑是。这个方法将从 API 调用中调用:

 public List<Album> getAllMusic() {
        try
             {
             Properties props = new Properties();
            props.load(new FileInputStream("src/main/resources/log4j.properties"));
            PropertyConfigurator.configure(props); 

       List<Album> albumList = (List) cache.get("musicCache");
     System.out.println("Size of album is"+albumList.size());
        if(albumList!=null) {
            System.out.println("Returning data from cache");
          return albumList;
           }
     } catch(Exception e ){

             try{
                 System.out.println("Putting data in cache");
                 musicDao.putIncache();
             }
             catch(Exception ef){

             }
             return musicDao.getAllTopHundredMusic();
     }

        return null;

    }

首先,我将使用以下命令检查缓存:

List<Album> albumList = (List) cache.get("musicCache");

在第一种情况下,它总是空的,所以我将调用 DAO 层,同时将数据放入缓存中,即:

System.out.println("Putting data in cache");
                 musicDao.putIncache();

因此,数据被加载到缓存中,但是当我再次点击 URL 时,缓存仍然为空,它再次点击 DAO 层(如第二次点击,它需要从缓存中获取数据而不是从 DAO)。我尝试检查我的缓存大小,但它总是空的。当我将数据放入缓存时,它被添加到缓存中,但是当我尝试检索它时,它总是空的。将所有数据放入缓存的逻辑在DAO层是:

public void putIncache() {
        try {


            CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
            Properties props = new Properties();

            props.put("jcs.default","DC");
            props.put("jcs.default.cacheattributes",
                    "org.apache.jcs.engine.CompositeCacheAttributes");
// lots more props.put - this is basically the contents of cache.ccf
            Logger log
                    = Logger.getLogger(MusicDao.class);

            ccm.configure(props);

            // Load the cache
            cache = JCS.getInstance("musicCache");


            int i=0;
            for(Album a:albumList){
                cache.put(new Integer(i+1), a);

            }


        } catch (CacheException e) {
            System.out.println("here");
            e.printStackTrace();
        }
    }

我现在正在输入我的完整代码:我的模型类是:

package com.ashwin.cacheapp.model;

import java.io.Serializable;

public class Album implements Serializable {
    private Integer id;
    private String artist;
    private String title;

    public Album() {
    }

    public Album( Integer id, String artist, String title ) {
        this.id = id;
        this.artist = artist;
        this.title = title;
    }

    public Integer getId() {
        return id;
    }

    public void setId( Integer id ) {
        this.id = id;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist( String artist ) {
        this.artist = artist;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle( String title ) {
        this.title = title;
    }

    public String toString() {
        return artist + ": " + title;
    }
}

APi 调用通过这个类发生:

package com.ashwin.cacheapp;

import com.ashwin.cacheapp.model.Album;
import com.ashwin.cacheapp.response.ResponseGen;
import com.ashwin.cacheapp.service.MusicService;

import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("hello")
@RequestScoped
public class ApiResource {

    MusicService musicService=new MusicService();


    @GET
    @Path("getAllMusic")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Album> getAllMusic() {
        List<Album> albumList=musicService.getAllMusic();

        return musicService.getAllMusic();

    }


}

音乐服务.java

package com.ashwin.cacheapp.service;

import com.ashwin.cacheapp.dao.MusicDao;
import com.ashwin.cacheapp.model.Album;

import org.apache.jcs.JCS;

import java.util.List;

import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.util.Properties;

//@Stateless
public class MusicService {

    private MusicDao musicDao=new MusicDao();
  private JCS cache;


    public List<Album> getAllMusic() {
        try
             {
             Properties props = new Properties();
            props.load(new FileInputStream("src/main/resources/log4j.properties"));
            PropertyConfigurator.configure(props); 

       List<Album> albumList = (List) cache.get("musicCache");
     System.out.println("Size of album is"+albumList.size());
        if(albumList!=null) {
            System.out.println("Returning data from cache");
          return albumList;
           }
     } catch(Exception e ){

             try{
                 System.out.println("Putting data in cache");
                 musicDao.putIncache();
             }
             catch(Exception ef){

             }
             return musicDao.getAllTopHundredMusic();
     }

        return null;

    }
}

道类是:(我使用的是虚拟数据)

package com.ashwin.cacheapp.dao;


import com.ashwin.cacheapp.model.Album;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.log4j.Logger;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


public class MusicDao {
  private List<Album> albumList=new ArrayList<Album>();
    private JCS cache;

    public List<Album> getAllMusic() {
           return null;

    }

    public List<Album> getAllTopHundredMusic() {


        albumList.add(new Album(1, "Toby Mac", "Diverse City"));
        albumList.add(new Album(2, "Toby Mac", "Diverse City"));
        albumList.add(new Album(3, "Toby Mac", "Diverse City"));
        albumList.add(new Album(4, "Toby Mac", "Diverse City"));
        albumList.add(new Album(5, "Toby Mac", "Diverse City"));
        albumList.add(new Album(6, "Toby Mac", "Diverse City"));
        albumList.add(new Album(7, "Toby Mac", "Diverse City"));
        albumList.add(new Album(8, "Toby Mac", "Diverse City"));
        albumList.add(new Album(9, "Toby Mac", "Diverse City"));
        albumList.add(new Album(10, "Toby Mac", "Diverse City"));
        albumList.add(new Album(11, "Toby Mac", "Diverse City"));
        albumList.add(new Album(12, "Toby Mac", "Diverse City"));
        albumList.add(new Album(13, "Toby Mac", "Diverse City"));
        albumList.add(new Album(14, "Toby Mac", "Diverse City"));
        albumList.add(new Album(15, "Toby Mac", "Diverse City"));
        albumList.add(new Album(16, "Toby Mac", "Diverse City"));
        albumList.add(new Album(17, "Toby Mac", "Diverse City"));
        albumList.add(new Album(18, "Toby Mac", "Diverse City"));
        albumList.add(new Album(19, "Toby Mac", "Diverse City"));

       return albumList;

    }

    public void putIncache() {
        try {


            CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
            Properties props = new Properties();

            props.put("jcs.default","DC");
            props.put("jcs.default.cacheattributes",
                    "org.apache.jcs.engine.CompositeCacheAttributes");
// lots more props.put - this is basically the contents of cache.ccf
            Logger log
                    = Logger.getLogger(MusicDao.class);

            ccm.configure(props);

            // Load the cache
            cache = JCS.getInstance("musicCache");


            int i=0;
            for(Album a:albumList){
                cache.put(new Integer(i+1), a);

            }


        } catch (CacheException e) {
            System.out.println("here");
            e.printStackTrace();
        }
    }
}

src/main/resource我有配置文件 cache.ccf 为:

jcs.default=
jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=200
jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=86400
jcs.default.elementattributes.IdleTime=86400

我不知道这个文件是否是从类路径调用的,所以我在我的 pom.xml 中添加了:

 <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.ccf</include>
        </includes>
      </resource>
        </resources>

在 pom.xml 中是:

<?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>

    <groupId>com.ashwin</groupId>
    <artifactId>CacheApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>CacheApp</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>  
        <repository>
    <id>jfrog-libs</id>
    <name>jfrog-libs</name>
    <url>http://repo.jfrog.org/artifactory/libs-releases</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-jcs-core</artifactId>
            <version>2.2.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

         <!-- https://mvnrepository.com/artifact/jcs/jcs -->
    <dependency>
      <groupId>jcs</groupId>
      <artifactId>jcs</artifactId>
      <version>1.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/concurrent/concurrent -->
    <dependency>
      <groupId>concurrent</groupId>
      <artifactId>concurrent</artifactId>
      <version>1.3.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
</dependency>


<dependency>
    <groupId>com.google.guava</groupId> 
    <artifactId>guava</artifactId> 
    <version>16.0</version> 
</dependency>


    </dependencies>

    <build>

        <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.ccf</include>
        </includes>
      </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我调用 api 时的输出http://localhost:8080/api/hello/getAllMusic是:

Putting data in cache 
Putting data in cache 
Putting data in cache 
Putting data in cache 

我的预期输出是:

 Putting data in cache 
Returning data from cache
Returning data from cache
Returning data from cache
Returning data from cache

编辑源代码:我创建了新类 MusicClass.java

package com.ashwin.cacheapp.dao;

import com.ashwin.cacheapp.model.Album;
import com.ashwin.cacheapp.model.Student;
import java.util.Properties;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.log4j.Logger;



public class MusicCache {
     private JCS cache;


       // static variable single_instance of type Singleton 
    private static MusicCache single_instance=null; 


    // private constructor restricted to this class itself 
    private MusicCache() 
    { 
          try {
           CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
            Properties props = new Properties();

            props.put("jcs.default","DC");
            props.put("jcs.default.cacheattributes",
                    "org.apache.jcs.engine.CompositeCacheAttributes");
// lots more props.put - this is basically the contents of cache.ccf
            Logger log
                    = Logger.getLogger(MusicDao.class);

            ccm.configure(props);

            // Load the cache
            cache = JCS.getInstance("musicCache");

            cache.put( "123", new Student( 123, "John", "Swizher", "Weige", "Civil") );
            cache.put( "143", new Student( 143, "Theoder", "", "Sattler", "Computer Science" ) );
            cache.put( "153", new Student( 153, "Martin", "Sam", "Suckhorozka", "Electrical" ) );
            cache.put( "163", new Student( 163, "Russel", "", "Latour", "Mechanical" ) );


        } catch (CacheException e) {
            System.out.println("here");
            e.printStackTrace();
        }
    } 

    // static method to create instance of Singleton class 
    public static MusicCache Singleton() 
    { 
        // To ensure only one instance is created 
        if (single_instance == null) 
        { 
            single_instance = new MusicCache(); 
        } 
        return single_instance; 
    } 
}

服务类功能的变化是:

  public  List<Album> getAllMusic() {
        try
             {
             Properties props = new Properties();
            props.load(new FileInputStream("src/main/resources/log4j.properties"));
            PropertyConfigurator.configure(props); 

     List<Album> albumList = (List) cache.get("musicCache");
     System.out.println("Size of album is"+albumList.size());
        if(albumList!=null) {
            System.out.println("Returning data from cache");
          return albumList;
           }
     } catch(Exception e ){

             try{
                 System.out.println("Putting data in cache");
                 MusicCache x = MusicCache.Singleton();
             }
             catch(Exception ef){

             }
             return musicDao.getAllTopHundredMusic();
     }

        return null;

    }

我仍然得到相同的输出

4

0 回答 0