001// ______________________________________________________ 002// Generated by sql2java - https://github.com/10km/sql2java-2-6-7 (custom branch) 003// modified by guyadong from 004// sql2java original version https://sourceforge.net/projects/sql2java/ 005// JDBC driver used at code generation time: com.mysql.jdbc.Driver 006// template: itablecache.java.vm 007// ______________________________________________________ 008package net.gdface.facelog.db; 009 010import java.util.Map; 011import java.util.concurrent.ConcurrentMap; 012import java.util.concurrent.TimeUnit; 013 014import net.gdface.facelog.db.exception.ObjectRetrievalException; 015/** 016 * 数据库对象缓存接口 017 * @param <K> 主键类型(Primary or Unique) 018 * @param <B> 数据库记录对象类型(Java Bean) 019 * @author guyadong 020 */ 021public interface ITableCache<K, B extends BaseBean<B>> { 022 class ImmutableEntry<K,V> implements Map.Entry<K,V>{ 023 private K key; 024 private V value; 025 public ImmutableEntry(K key) { 026 this.key = key; 027 } 028 public ImmutableEntry(K key, V value) { 029 this(key); 030 this.value = value; 031 } 032 @Override 033 public final K getKey() { 034 return key; 035 } 036 037 @Override 038 public final V getValue() { 039 try { 040 return (V) reload(); 041 } catch(ObjectRetrievalException e){ 042 return null; 043 }catch (RuntimeException e) { 044 throw e; 045 } catch (Exception e) { 046 throw new RuntimeException(e); 047 } 048 } 049 @Override 050 public final V setValue(V value) { 051 throw new UnsupportedOperationException(); 052 } 053 protected V reload()throws Exception{ 054 return value; 055 } 056 } 057 /** 058 * Update strategy for cache 059 */ 060 public static enum UpdateStrategy{ 061 /** update no matter whether key exists */ 062 always, 063 /** update only if key exists */ 064 replace, 065 /** remove key */ 066 remove, 067 /** reload data if key exists, need {@code entry } implement the reload method */ 068 refresh; 069 /** 070 * update {@code entry} to {@code map},if {code getValue()} return {@code null},remove key. 071 */ 072 public <K,V> void update(ConcurrentMap<K,V> map,ImmutableEntry<K,V>entry){ 073 if(null == map || null == entry ){ 074 return ; 075 } 076 K key = entry.getKey(); 077 if( null == key){ 078 return; 079 } 080 V value = entry.getValue(); 081 if(null == value){ 082 map.remove(key); 083 return ; 084 } 085 switch(this){ 086 case always: 087 map.put(key, value); 088 break; 089 case replace: 090 map.replace(key, value); 091 break; 092 case remove: 093 map.remove(key); 094 break; 095 case refresh: 096 map.replace(key, value); 097 default: 098 break; 099 } 100 } 101 } 102 public static final UpdateStrategy DEFAULT_STRATEGY = UpdateStrategy.always; 103 public static final long DEFAULT_CACHE_MAXIMUMSIZE = 10000; 104 public static final long DEFAULT_DURATION = 10; 105 public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MINUTES; 106 /** 107 * 加载主键(key)指定的记录,如果缓存中没有则从数据库中查询<br> 108 * 数据库中没有找到则抛出异常 109 * @param key 110 * @return 111 * @throws Exception 112 */ 113 public B getBean(K key) throws Exception; 114 /** 115 * 加载主键(key)指定的记录,如果缓存中没有则从数据库中查询<br> 116 * 数据库中没有找到则返回{@code null} 117 * @param key 118 * @return 119 */ 120 public B getBeanUnchecked(K key); 121 /** 122 * 返回cache中{@code key}指定的记录,如果不存在就返回{@code null} 123 * @param key 124 * @return return false if key is null 125 */ 126 public B getBeanIfPresent(K key); 127 /** 128 * 删除cache中{@code key}指定的记录 129 * @param bean 130 */ 131 public void remove(B bean); 132 /** 133 * 向cache中更新数据 134 * @param bean 135 * @see UpdateStrategy 136 */ 137 public void update(B bean); 138 /** 注册侦听器 */ 139 public void registerListener(); 140 /** 注销侦听器 */ 141 public void unregisterListener(); 142}