在我的应用程序中,我使用 ehcache 二级缓存。在该应用程序中,我在向 db 添加一些数据后删除了 ehcache。在这里我发送了我的 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"
/>
<cache
name="com.model.Customer"
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"
/>
<cache
name="com.model.Friend"
maxElementsInMemory="100000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
现在我想使用此代码删除 cahche
CacheManager manager = CacheManager.getInstance();
Cache cache = manager.getCache();
cache.removeAll();
所以我需要缓存名称。那么如何获得我的缓存名称?请帮助我任何一个是的缓存配置正确。这是我插入数据的java代码
Transaction trns = null;
Session session = HibernateUtil.getFirstFactory().openSession();
try
{
Customer cus=new Customer();
cus.setName(name);
cus.setMobile(Long.parseLong(mno));
trns = session.beginTransaction();
Query query= session.createQuery("from Customer where name=? or mobile=?");
query.setParameter(0, cus.getName());
query.setParameter(1, cus.getMobile());
cus=(Customer)query.uniqueResult();
if(cus==null)
{
cus=new Customer(name,Long.parseLong(mno),f);
session.save(cus);
session.getTransaction().commit();
}
}
catch (Exception e)
{
if(trns != null){
trns.rollback();
}
e.printStackTrace();
} finally{
CacheManager manager = CacheManager.getInstance();
String[] names = manager.getCacheNames();
System.out.println("length="+names.length);//here the output is length=0
for (int i=0;i<names.length;i++)//so the control terminate the loop so the cache does not remove anything
{
System.out.println("name="+names[i]);
Cache cache = manager.getCache(names[i]);
cache.removeAll();
}
session.flush();
session.close();
}
这是我的客户班
private Long cid;
private String name;
private Long mobile;
private Set<Friend> friends = new HashSet<Friend>(0);
public Customer(String name, Long mobile,Set<Friend> friends) {
this.name = name;
this.mobile = mobile;
this.friends=friends;
}
public Customer() {}
public Long getCid() {
return cid;
}
public void setCid(Long cid) {
this.cid = cid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getMobile() {
return mobile;
}
public void setMobile(Long mobile) {
this.mobile = mobile;
}
public Set<Friend> getFriends() {
return friends;
}
public void setFriends(Set<Friend> friends) {
this.friends = friends;
}
这是我的 Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.model.Customer" table="customer">
<cache usage="read-write"/>
<id column="cid" name="cid" type="java.lang.Long" >
<generator class="native"/>
</id>
<property name="name" />
<property name="mobile" index="msisdn"/>
<!-- <set name="cards" table="t_customer_card" cascade="all">
<key column="code" not-null="true"/>
<many-to-many class="com.asta.model.Card" column="cid" unique="true" />
</set> -->
<set name="friends" table="customer_friends" cascade="all" lazy="false">
<key column="cid" not-null="true"/>
<many-to-many class="com.model.Friend" column="fid" unique="true" />
</set>
</class>
</hibernate-mapping>
我不知道我错在哪里?