<!--[if !supportEmptyParas]--> <!--[endif]--><?xml:namespace prefix = o />

一、修改 DiskPersistenceListener.java 文件:

<!--[if !supportEmptyParas]--> <!--[endif]-->

package com.jh.xh.hibernate.oscache;

<!--[if !supportEmptyParas]--> <!--[endif]-->

import com.opensymphony.oscache.plugins.diskpersistence.AbstractDiskPersistenceListener;

<!--[if !supportEmptyParas]--> <!--[endif]-->

public class DiskPersistenceListener extends AbstractDiskPersistenceListener {

<!--[if !supportEmptyParas]--> <!--[endif]-->

    private static final long serialVersionUID = 1L;

<!--[if !supportEmptyParas]--> <!--[endif]-->

    private static final String CHARS_TO_CONVERT = "./\\ :;\"\'_?" ;

<!--[if !supportEmptyParas]--> <!--[endif]-->

    public char [] getCacheFileName(String key) { // <1> protected 改为 public

       if ((key == null ) || (key.length() == 0)) {

           throw new 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 = new char [sb.length()];

        sb.getChars(0, fileChars. length , fileChars, 0);

        return fileChars;

    }

<!--[if !supportEmptyParas]--> <!--[endif]-->

    public static void 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]-->