<!--[if !supportEmptyParas]--> <!--[endif]-->
一、修改 DiskPersistenceListener.java 文件:
<!--[if !supportEmptyParas]--> <!--[endif]-->
package com.jh.xh.hibernate.oscache;
<!--[if !supportEmptyParas]--> <!--[endif]-->
import com.opensymphony.oscache.plugins.diskpersistence.AbstractDiskPersistenceListener;
<!--[if !supportEmptyParas]--> <!--[endif]-->
publicclass DiskPersistenceListener extends AbstractDiskPersistenceListener {
<!--[if !supportEmptyParas]--> <!--[endif]-->
privatestaticfinallongserialVersionUID = 1L;
<!--[if !supportEmptyParas]--> <!--[endif]-->
privatestaticfinal String CHARS_TO_CONVERT = "./// :;/"/'_?";
<!--[if !supportEmptyParas]--> <!--[endif]-->
publicchar[] getCacheFileName(String key) { // <1> protected 改为 public
if ((key == null) || (key.length() == 0)) {
thrownew IllegalArgumentException("Invalid key '" + key
+ "' specified to getCacheFile.");
}
<!--[if !supportEmptyParas]--> <!--[endif]-->
char[] chars = key.toCharArray();
<!--[if !supportEmptyParas]--> <!--[endif]-->
StringBuffer sb = new StringBuffer(chars.length + 10);
<!--[if !supportEmptyParas]--> <!--[endif]-->
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
int pos = CHARS_TO_CONVERT.indexOf(c);
<!--[if !supportEmptyParas]--> <!--[endif]-->
if (pos >= 0) {
sb.append('_');
sb.append(i);
} else {
sb.append(c);
}
if (i % 3 == 2) { // <2> 添加此行代码,每三个字符做为目录名;
sb.append('/');
}
}
sb.append('0');
char[] fileChars = newchar[sb.length()];
sb.getChars(0, fileChars.length, fileChars, 0);
return fileChars;
}
<!--[if !supportEmptyParas]--> <!--[endif]-->
publicstaticvoid main(String str[]) {
System.out.println(new DiskPersistenceListener().getCacheFileName("12341dfkaklakasdfadklskadllkklasdf"));
<!--[if !supportEmptyParas]--> <!--[endif]-->
}
}
<!--[if !supportEmptyParas]--> <!--[endif]-->
<!--[if !supportEmptyParas]--> <!--[endif]-->
二、修改 oscache.properties 文件:
cache.persistence.class=com.jh.xh.hibernate.oscache.DiskPersistenceListener
<!--[if !supportEmptyParas]--> <!--[endif]-->