SwarmCache Tutorial
全面了解一种开源产品,首要就是看它的文档。懵懵懂懂的读一遍英文文档随后既忘,翻译一遍会加深记忆:
 
How It WorksSwarmCache背后的概念很简单。每个服务器都有自己的管理器。对于每一种对象,服务器都要实例化一种cache并把它加到管理器中。The concept behind SwarmCache is fairly simple.Each server instantiates its own manager. 
For each type of object that the server wishes to cache, it instantiates a cache and adds it to the manager. 
  
管理器加入一个多播组并和组内的其他管理器通信。当一个对象从cache内移出时,管理器通知组内的其他管理器。被通知的管理器就会确保那个对象从他们各自的cache里移出。这样的结果是在其他服务器删除或更新缓存对象时,各个服务器的cache里不会保留陈旧版本的缓存对象。
 
The manager joins a multicast group and communicates with other managers in the group. Whenever an object is removed from a cache, the manager notifies all other managers in the group. Those managers then ensure that the object is removed from their respective caches. The result is that a server will not have in its cache a stale version of an object that has been updated or deleted on another server.
 
注意,管理器之间只有在一个缓存对象被移出时才会相互通信,这只发生在对象被更新或删除时。除此之外服务器之间不会协作 (注,既然文档这么说,猜测到在缓存的对象作更新时,管理器有先从cache里移出旧对象再加入新对象的动作)。这意味着服务器之间的通信次数是和应用的更新/删除次数成比例的。由于是以多播的方式进行通信所以它不会和host的数量有比例关系。
 
Note that the managers only need to communicate when an object is removed from a cache. This only happens when an object is updated or deleted. The managers do not cooperate beyond this. This means that the amount of inter-server communications is proportional to the amount of updates/deletes of the application. As the communications is multicast, it is not proportional to the number of caching hosts. 
 
还请注意这里没有什么"server",所有的host都是彼此同等地位的,"server"可以随意光临cache group而不影响其他组成员。(注,大概意思应该是,任何一个host都随时可以充当服务器,广播给其他host。)
Also notice that there is no "server"; all hosts are equal peers and they can come and go from the cache group as they please without affecting other group members. 
Thus the operation of the distributed cache is very robust. Basic UsageSwarmCache是为整合到持久层为数据库的java应用之中而设计的。也对其他类型的应用有帮助。当把它潜入到持久层引擎时,它的使用对更层的应用来说是透明的。SwarmCache并不直接支持事务管理,但可以通过封装缓存对象和附加事务数据来完成。这可能以后会实现。(注,应该是实现通过后者方法的事务管理办法)
 
SwarmCache is designed to be integrated in to the persistence layer of a database-driven Java application. However, it could be useful for other types of applications. Once built in to the persistence engine, SwarmCache should be transparent to higher layers of the software. SwarmCache does not directly support transaction management. This can be accomplished by wrapping the cached objects and storing some additional transaction data. This will described and possibly implemented at a later date.
 
Swarmcache要求在你的classpath里有swarmcache.jar, jgroups-all.jar, commons-collections.jar, and commons-logging.jar。
Swarmcache requires that swarmcache.jar, jgroups-all.jar, commons-collections.jar, and commons-logging.jar (included in the download) be in your classpath.
 
对于大多数的应用,使用和配置SwarmCache用CacheFactory类就足够了。示例:For most applications, it is sufficient to make use of the CacheFactory class to configure and use SwarmCache. Here is an example: 
import net.sf.swarmcache.*;
...
CacheFactory cacheFactory;
...
// Configure and Initialize the cache manager
CacheConfiguration conf = new CacheConfiguration();
conf.setCacheType(CacheConfiguration.TYPE_LRU);
cacheFactory = new CacheFactory(conf);
...
// Create a new cache, using a name that describes
//  what kind of object we will store
ObjectCache cache = cacheFactory.createCache("People");
...
// Store something in the cache
String key = "0001";
String person = "John Watkinson";
cache.put(key, person);
...
// Retrieve something from the cache
String person = (String)cache.get("0001");
System.out.println(person);
...
// Clear an object from the cache
// (This results in the sending of a 
//  multicast message to other caching hosts)
cache.clear("0001");
String person = (String)cache.get("0001");
// The following will print 'null'
System.out.println(person);
 
例子很简单,但局域网内的多台机器运行相同的代码会使他们的缓存保持一致。
The example is simple, but running this same code on multiple machines in a network will result in their caches being consistent.
注意keys必须是serializable对象,需要implement java.io.Serializable。存在cache里的对象本身并不需要是serializable的。(注,可以得知SwarmCache不会对缓存对象保存到硬盘上) 当然,为了传输效率keys不应该太大。
 
Note that the keys used must be serializable objects-- they must implement java.io.Serializable. The objects themselves that are stored in the cache need not be serializable. And of course, the keys should not be large objects for transmission efficiency purposes.
 
为了保持keys的独特性,通常是每种类型的对象一个cache。在这种情况下, 请遵守约定为缓存的每个类型的对象利用类的完全限定名来命名.In order to keep keys distinct, it is usually preferred to have one cache per type of object stored. In that case, it makes sense to follow the convention that the cache be named after the fully-qualified class name of the object type to be stored. Usage in a Persistence Engine
 
这里的代码片断示例了如何把SwarmCache整合到持久层引擎中.Here is some skeleton code that demonstrates how SwarmCache could be integrated in to a persistence engine. The following class is responsible for persisting an object of type Person:
public class PersonEntity extends GenericEntity {
 ObjectCache cache;
 
 public PersonEntity(CacheFactory cacheFactory) {
  cache = cacheFactory.createCache("Person");
  // * Other initialization here
 }
 ...
 
 public Person get(long key) {
  Long cacheKey = new Long(key);
  Person person = (Person)cache.get(cachekey);
  if (person == null) {
   // * Get the object from the database   
   if (person != null) {
    // Put it in the cache
    cache.put(cacheKey, person);
   }
  }
  return person;
 }
 
 ...
 
 public void insert(Person person) {
  // * Do database insert
  // Add new object to cache
  Long cacheKey = new Long(person.getKey());
  cache.put(cacheKey, person);
 }
 
 ...
 
 public void update(Person person) {
  // * Do database update
  // Remove object from cache
  // (This causes all caches in the group to be notified)
  Long cacheKey = new Long(person.getKey());
  cache.clear(cacheKey);
  // Re-add the object to the cache
  cache.put(cacheKey, person);
 }
 
 ...
 
 public void delete(long key) {
  // * Do database delete
  // Remove object from cache
  // (This causes all caches in the group to be notified)
  Long cacheKey = new Long(key);
  cache.clear(cacheKey);  
 }
 
 ...
}

解决方案 »

  1.   

    我想知道的是具体的方法,比如说我在一个应用程序里面使用
    CacheConfiguration conf = CacheConfigurationManager.getConfig();

    CacheFactory factory = new CacheFactory(conf);

    MultiCacheManager cacheManager = factory.getCacheManager();

    ObjectCache cache = (MultiCache) factory.createCache("TEST");
    cache.put("list1", "String 1");
    加入了cache,这个程序的其它部分可以通过System.out.println(cache.get("list1"));得到要"String 1"
    可是另外一个独立的应用程序要怎么写才能读到这个cache