// 上一篇:必经之地(using)

// 下一篇:程序计数器(PC)


基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构。

There are only two hard things in computer science: cache invalidation and naming things.

--Phil Karlton

前情回顾

上一次,我们写了资源打开关闭自动化的控制结构。有熟悉ObjectC语言的朋友补充了一个苹果的做法: autorelease pool

比如说Lock,可以这么声明:

@autoreleasepool{
id lock = [Lock lock]; // autorelease 对象, 创建后 lock, dealloc 时释放
}

它的主要作用是,延迟对象的释放。对一个对象调用 autorelease 方法,那么它就被保存到最里层的 autorelease pool。当 autorelease pool 块结束的时候,对里面所有的对象发送一次 release。Objective-C 里有大量的对象都是通过 autorelease pool 管理。

也可以通过autoreleasepool做性能优化:

for(int i=0; i<100; i++){
create_lots_autoreleased_objects();
}

这段代码可以被优化成:

for(int i=0; i<100; i++){
@autoreleasepool{
create_lots_autoreleased_objects();
}
}

这个相当于是加速了内存的释放流程,降低了峰值内存占用。那么,其他语言有这种便利么?

典型代码

function send(pkg, onComplete){
let connection = new Connection(name);
connection.open(function(err){
connection.send(pkg, function(err){
connection.close();
onComplete();
});
})
}

结构分析

又是open,close。当然可以使用前一节讲到的方法。但这不是本节的主题,前一节实际上做了一个潜在假设,就是存在一个连接池子,下面的open和close里面获取资源和释放资源都是从资源池里取出和放回的:

open(onComplete) {
let self = this;
/**get from pool*/
self.m_database.getPOOL().getConnection(function(err, conn) {
if (err) {
return onComplete(err);
}
self.m_conn = conn;
onComplete(0);
});
} close() {
let self = this;
/**release to pool*/
self.m_conn.release();
}

如果你的资源不是个数据库,例如一个TCP连接,那么open和close是实实在在的打开和关闭连接,频繁操作就有性能问题。所以我们需要自己定制一个连接池。例如下面的实现:

class ConnectionPool{
constructor(capacity,ttl){
this.m_pool = {};
this.m_capacity = capacity;
this.m_ttl = ttl;
} init(){
let self = this; base.setTimer(function(){
self.trimeByTimeout();
}, 30*1000);
} trimeByTimeout(){
let self = this; /**剔除超时的连接*/
for(let name in self.m_pool){
let item = self.m_pool[name];
let elapse = base.getNow()-item.updateTime;
if(elapse>self.m_ttl){
self.remove(item);
}
}
} getConnection(name,onComplete){
let self = this; let item = self.peek();
self.open(item,(err){
onComplete(err, item.conn);
});
} peek(name){
let self = this; let cache = self.getCache(name);
if(cache){
return cache;
} self.trimeByLRU(); let conn = self.create(name);
let item = self.insert(name, conn); return item;
} getCache(name){
let self = this; /**获取缓存,也可以加入使用频次的统计,根据使用频次来选取*/
let cache = self.m_pool[name];
if(cache){
cache.updateTime = base.getNow();
return cache;
}else{
return null;
}
} trimeByLRU(){
let self = this; /**剔除最不活跃的连接*/
if(self.overflow()){
let minItem = self.findMinItem();
if(minItem!=null){
self.remove(minItem);
}
}
} overflow(){
let self = this; let length = Object.keys(self.m_pool).length;
return length > self.m_capacity;
} findMinItem(){
let self = this; let min = Infinity;
let minItem = null;
for(let name in slef.m_pool){
let item = self.m_pool[name];
if(item.updateTime<min){
min = item.upddateTime;
minItem = item;
}
}
return minItem;
} create(name){
let conn = new Connection(name);
return conn;
} open(item, onComplete){
let self = this; if(item.refCount===0){
item.conn.open(onComplete);
}else{
item.refCount++;
onComplete(0);
}
} release(item){
let self = this; item.refCount--;
if(item.refCount===0){
if(item.markRemove){
delete self.m_pool[item.name];
item.conn.close();
}
}else{
// ignore
}
} insert(name, conn){
let self = this; let item = {
updateTime: base.getNow(),
conn: conn,
refCount: 0,
markRemove: false,
name: name,
} /**给conn添加release方法*/
item.conn.release = function(){
self.release(item);
} self.m_pool[name] = item;
return item;
} remove(item){
let self = this; if(item.refCount>0){
item.markRemove = true;
}else{
delete self.m_pool[item.name];
item.conn.close();
}
}
}

有了连接池,就可以像常规一样使用Connection:

function test(){
let pool = new ConnectionPool(32,30*1000);
pool.init();
pool.getConnection(function(err, conn)=>{
conn.connect(function(err){
//...
conn.release();
})
});
}

语义分析

Least Recently Used, 简称LRU,是一种常用的缓存思路。基本思想是从资源池里剔除最近最少使用的资源条目。

编程中经常有优化的需求,例如缓存。但是很容易出现所谓的碎片优化,例如在需要使用资源的地方,每个地方都去手工持有资源的缓存,就不是一种好的方法,如果有多个协作者,还会导致每个人都自己做了一份缓存,或者在源代码的多个地方对同一类资源重复做缓存。

正确的做法应该是:

  1. 使用资源的地方保持资源使用的最简单形式:打开,使用,关闭(当然你可以使用自动化手法)
  2. 提供统一并且单点的资源池实现,在一个地方把资源的缓存做好。

控制结构(6): 最近最少使用(LRU)的更多相关文章

  1. 控制结构(6) 最近最少使用(LRU)

    // 上一篇:必经之地(using) // 下一篇:程序计数器(PC) 基于语言提供的基本控制结构,更好地组织和表达程序,需要良好的控制结构. There are only two hard thin ...

  2. Redis学习笔记2-使用 Redis 作为 LRU 缓存

    当 Redis 作为缓存使用时,当你添加新的数据时,有时候很方便使 Redis 自动回收老的数据.LRU 实际上是被唯一支持的数据移除方法.Redis 的 maxmemory 指令,用于限制内存使用到 ...

  3. Redis - 作为 LRU 缓存

    一.简介 LRU 实际上是被唯一支持的数据移除方法,同时也是 memcached 默认支持的缓存算法. 二.配置内存大小 在 redis.conf 文件中使用 maxmemory 指令能够配置内存大小 ...

  4. LeetCode解题报告:LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  5. 控制结构(7) 程序计数器(PC)

    // 上一篇:最近最少使用(LRU) // 下一篇:线性化(linearization) 程序的每一行都是一个状态,对应的行指令.同步的情况下同一个pc一直自增,异步的时候,分裂出一个新的子pc,独立 ...

  6. Redis作为lru缓存作用

    当 Redis 作为缓存使用时,当你添加新的数据时,有时候很方便使 Redis 自动回收老的数据.LRU 实际上是被唯一支持的数据移除方法.Redis 的 maxmemory 指令,用于限制内存使用到 ...

  7. 数据结构与算法之美 06 | 链表(上)-如何实现LRU缓存淘汰算法

    常见的缓存淘汰策略: 先进先出 FIFO 最少使用LFU(Least Frequently Used) 最近最少使用 LRU(Least Recently Used) 链表定义: 链表也是线性表的一种 ...

  8. mysql5.5手册读书日记(1)

    <?php //mysql语句使用技巧 /* * 我的数据库是5.5.2 * * 查询当前用户的登陆的名字 * select user(); * * 查询当前mysql服务器时间和服务器版本 * ...

  9. feilong's blog | 目录

    每次把新博客的链接分享到技术群里,我常常会附带一句:蚂蚁搬家.事实上也确实如此,坚持1篇1篇的把自己做过.思考过.阅读过.使用过的技术和教育相关的知识.方法.随笔.索引记录下来,并持续去改进它们,希望 ...

随机推荐

  1. .Net Core 实践 - 使用log4net记录日志(3)— log4net向ElasticSearch写日志

    demo地址:https://github.com/PuzzledAlien/log4net_demo/tree/master/DotNetCoreConsole_V3 Windows 10 安装部署 ...

  2. js如何使用正则表达式实现过滤HTML标签?(/<[^<>]+>/g)

    js如何使用正则表达式实现过滤HTML标签?(/<[^<>]+>/g) 一.总结 js进阶正则表达式实现过滤HTML标签(<>标签中不能包含标签实现过滤HTML标签 ...

  3. Html和Css学习笔记-html进阶-div与span

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 此篇博客是我的复习笔记,html和css学的时间太久了,忘得差不多了,最近要使用一下,所以重新打开html的书略读,后记录了标签 ...

  4. Dynamics 365执行操作报SQL Server已超时,更改这个超时设置的方法

    本人微信公众号:微软动态CRM专家罗勇 ,回复291或者20190110可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 当执 ...

  5. iOS 10 设备权限问题(相机,相册等)

    iOS 10 的设备权限 需要添加的字段有: <!-- 相册 --> <key>NSPhotoLibraryUsageDescription</key> <s ...

  6. Easyui datagrid combobox输入框非法输入判断与事件总结

    datagrid combobox输入框非法输入判断与事件总结 by:授客 QQ:1033553122 测试环境 jquery-easyui-1.5.3 常见事件 onSelect // 选择下拉列表 ...

  7. Android为TV端助力 ViewTreeObserver(转载)

    Android ViewTreeObserver简介   一.结构 public final class ViewTreeObserver extends Object java.lang.Objec ...

  8. (最完美)小米平板3的USB调试模式在哪里开启的流程

    经常我们使用安卓手机链上电脑的时候,或者使用的有些应用软件比如我们公司营销小组经常使用的应用软件引号精灵,之前的老版本就需要开启usb调试模式下使用,现经常新版本不需要了,如果手机没有开启usb调试模 ...

  9. win10的react native 开发环境搭建,使用Android模拟器

    1.打开cmd的管理员模式,win+X,选择命令提示符(管理员)即可,运行如下命令: @"%SystemRoot%\System32\WindowsPowerShell\v1.0\power ...

  10. MySQL下perror工具查看System Error Code信息

      在MySQL数据库的维护过程中,我们有时候会在MySQL的错误日志文件中看到一些关于Operating system error的错误信息,例如在MySQL的错误日志里面,有时候会看到关于 Inn ...