jfinal ehcache缓存的使用

如果数据变化的频率很低,就可以通过缓存技术来优化网站的性能,比如jfinal整合的ehcache的使用,只需要两个设置,1 ehcache.xml 配置,2在插件配置configPlugin里面加一句me.add(new EhCachePlugin());


首先在插件配置中初始化

public void configPlugin(Plugins me) {
		
		//配置缓存插件
		me.add(new EhCachePlugin());
		
	}

配置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"/>

    <cache name="sampleCache1"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           overflowToDisk="true"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="100"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off"
            />
              
</ehcache>

详细配置参考说明

http://blog.csdn.net/mlitsn/article/details/1909192

 

代码调用

public class IndexController extends Controller{
	public void index(){
		 Map<String, Object> indexMap= CacheKit.get("sampleCache1", "index");
		    if (indexMap== null) {
		    	System.err.println("缓存不存在");
		          indexMap=new HashMap<String, Object>();
		          indexMap.put("test", Test.dao.find("select * from test"));
		          CacheKit.put("sampleCache1", "index", indexMap);
		          
		    }else{
				System.err.println("已经缓存");
		    }
		setAttr("indexMap", indexMap);
		
		renderJson();
		render("index.html");
		
	}
}




如果广大网友觉得这篇文章有用,可以用    666导航网   收藏起来,方便随时查看


如果广大网友觉得这篇文章有用,可以用     666导航网   收藏起来,方便随时查看
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Ehcache3 是一个基于 Java 的缓存框架,它支持分布式缓存和多级缓存等多种功能,同时也支持分布式锁。Ehcache3 中的分布式锁基于 JCache API 的标准接口实现,可以使用 Ehcache3 内置的 Cache 接口轻松实现分布式锁。 具体来说,我们可以通过以下步骤实现分布式锁: 1. 创建一个 Cache 实例 2. 使用 Cache.putIfAbsent() 方法尝试在 Cache 中插入一个锁对象 3. 如果插入成功,说明获得了锁,否则说明锁已经被其他线程占用了 4. 在代码执行完毕后,使用 Cache.remove() 方法释放锁对象 下面是示例代码: ``` import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.expiry.Duration; import org.ehcache.expiry.Expirations; import java.util.concurrent.TimeUnit; public class DistributedLock { private final Cache<String, Boolean> cache; public DistributedLock(CacheManager cacheManager) { this.cache = cacheManager.createCache("lock", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Boolean.class, ResourcePoolsBuilder.heap(1)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(10, TimeUnit.SECONDS)))); } public boolean tryLock(String key) { return cache.putIfAbsent(key, true) == null; } public void unlock(String key) { cache.remove(key); } } ``` 在上述代码中,我们首先创建了一个 Cache 实例,然后在 tryLock() 方法中尝试插入一个锁对象,如果插入成功,则返回 true,否则返回 false。在 unlock() 方法中,我们调用 Cache.remove() 方法来释放锁对象。 如果想要实现更高级的分布式锁功能,比如自旋等待、可重入锁、公平锁等,我们可以使用更专业的分布式锁库,比如 ZooKeeper 或者 Redisson 等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值