2
在我的應用我使用cache.in該應用程序我刪除了Ehcache的Ehcache第二級之後添加一些數據來db.here我把我的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();
,所以我需要緩存name.so將如何得到我的緩存的名字嗎?請幫我任何一個 是緩存的配置是否正確。這是我對插入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>
我不知道我錯在哪裏?
感謝您的回覆。 CacheManager manager = CacheManager.getInstance(); String [] names = manager.getCacheNames();我使用這段代碼,但輸出是長度= 0.因此,它沒有得到任何緩存名稱 – 2013-03-19 10:59:37
你是否設法最終破解它(作爲你接受了我的回答)?或者您的評論仍然適用? – mindas 2013-03-19 12:45:12
請寄給我如何獲得緩存名稱的完整代碼,我使用上面的代碼,但它不顯示任何緩存名稱。 – 2013-03-20 07:09:17