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