我尝试使用(JCS)实现java缓存系统。我从一些网站学习并尝试实现它们。这是我的一些配置:
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>jcsprojectone</artifactId>
<version>1.0-SNAPSHOT</version>
<name>jcsprojectone</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</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>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
主 app.java 类
package com.ashwin.jcsprojectone;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.io.FileInputStream;
import java.util.Properties;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.load(new FileInputStream("src/log4j.properties"));
PropertyConfigurator.configure(props);
MusicStore musicStore = new MusicStore();
musicStore.addAlbum(new Album(4, "The O.C. Supertones",
"Supertones Strike Back"));
Album album = musicStore.getAlbum(1);
System.out.println("Album 1: " + album);
album = musicStore.getAlbum(4);
System.out.println("Album 4: " + album);
musicStore.removeAlbum(4);
album = musicStore.getAlbum(4);
System.out.println("Album 4: " + album);
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
}
专辑.java
package com.ashwin.jcsprojectone;
public class Album implements java.io.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;
}
}
MusicStore.java
package com.ashwin.jcsprojectone;
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 java.util.Properties;
public class MusicStore {
private JCS cache;
public MusicStore() {
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(MusicStore.class);
// Load the cache
cache = JCS.getInstance("musicCache");
// Initialize the cache
cache.put(new Integer(1),
new Album(1, "Toby Mac", "Diverse City"));
cache.put(new Integer(2),
new Album(2, "Pillar", "Fireproof"));
cache.put(new Integer(3),
new Album(3, "Audio Adrenaline", "Underdog"));
} catch (CacheException e) {
e.printStackTrace();
}
}
public void addAlbum(Album album) {
try {
cache.put(album.getId(), album);
} catch (CacheException e) {
e.printStackTrace();
}
}
public Album getAlbum(Integer id) {
return (Album) cache.get(id);
}
public void removeAlbum(Integer id) {
try {
cache.remove(id);
} catch (CacheException e) {
e.printStackTrace();
}
}
}
我已将 cache.ccf 和 log4j.properties 文件配置为:
我不知道我是否将这些文件放在正确的路径中并正确加载了代码。起初,没有找到文件错误,所以我移动了 int src 文件夹并在代码中进行了配置。
缓存.ccf
# DEFAULT CACHE REGION
jcs.default=DC
jcs.default.cacheattributes=
org.apache.jcs.engine.CompositeCacheAttributes
jcs.default.cacheattributes.MaxObjects=1000
jcs.default.cacheattributes.MemoryCacheName=
org.apache.jcs.engine.memory.lru.LRUMemoryCache
jcs.default.cacheattributes.UseMemoryShrinker=true
jcs.default.cacheattributes.MaxMemoryIdleTimeSeconds=3600
jcs.default.cacheattributes.ShrinkerIntervalSeconds=60
jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes
jcs.default.elementattributes.IsEternal=false
jcs.default.elementattributes.MaxLifeSeconds=21600
jcs.default.elementattributes.IdleTime=1800
jcs.default.elementattributes.IsSpool=true
jcs.default.elementattributes.IsRemote=true
jcs.default.elementattributes.IsLateral=true
log4j.properties
log4j.rootLogger=DEBUG, DebugAppender
#Debug logging
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=activityLog.log
log4j.appender.DebugAppender.MaxFileSize=200KB
log4j.appender.DebugAppender.MaxBackupIndex=5
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d{DATE} %t - %m%n
但是我的项目运行成功,但 Null 在输出中打印为:
null
Process finished with exit code 0
除了这个值,输出中没有打印任何内容。为什么没有我的输出来?