working-usage's Introduction
working-usage's People
working-usage's Issues
tk mybatis usage Example.Criteria issues
Example example = new Example(ExamplePO.class);
Example.Criteria criteria = example.createCriteria();
if (StringUtils.isNotBlank(name)) {
criteria.orEqualTo("name", name);
}
if (StringUtils.isNotBlank(age)) {
criteria.orEqualTo("age", age);
}
// Example 1 is Bad 😞 composed SQL condition (name = ? or age= ? and active = ?)
criteria.andEqualTo("active", ApplicationConstants.ACTIVE);
// Example 2 is Good 😁 composed SQL condition 2 (name= ? or age = ? ) and (active = ?)
Example.Criteria againCriteria = example.createCriteria();
againCriteria.andEqualTo("active", ApplicationConstants.ACTIVE);
example.and(againCriteria);
Linux 常用命令
解压缩
#解压缩
$ tar xzvf file
$ unzip -o -d /opt/dirName/ file.zip
#压缩
$ tar czvf file
#example
$ xz -d node-v8.11.1-linux-x64.tar.xz
$ tar -xvf node-v8.11.1-linux-x64.tar.xz
$ tar -xvJf node-v8.11.1-linux-x64.tar.xz
linux 设置可执行文件的软链接
$ sudo ln -fs /usr/local/mysql/bin/mysql mysql
设置权限
$ chown -R ocsftp004:ocsftp004 /opt/sftp/ocsftp004/event/
检查磁盘
#查看机器磁盘使用情况
$ df -lk
#查看目录占用磁盘大小:
$ du --max-depth=1 -h
#查看文件占用磁盘大小
$ ls -lht
后台运行
使用&命令后,作业被提交到后台运行,当前控制台没有被占用,但是一但把当前控制台关掉(退出帐户时),作业就会停止运行。nohup命令可以在你退出帐户之后继续运行相应的进程。nohup就是不挂起的意思( no hang up)
$ mongodb &
------
$ nohup mongodb &
查找文件
#显示20分钟前的文件
$ find /opt/app/logs/ -name f -mmin +20 -exec ls -l {} \;
#删除20分钟前的文件
$ find /opt/app/logs/ -name f -mmin +20 -exec rm {} \;
#显示20天前的文件
$ find /opt/app/logs/ -name f -mtime +20 -exec ls -l {} \;
删除指定符合文件名的文件
$ find /opt/app/logs/ -name "*2019-12*.log" -exec rm {} \;
参考资料
防抖和节流
转至: https://github.com/webproblem/Blog/blob/master/前端/ECMAScript/防抖和节流.md
防抖和节流
在实际开发应用中,经常会碰到高频率的事件处理,比如 window 的 scroll, resize 以及 keyup,mousemove 等事件。这些高频率的事件触发会带来一些显著的问题。
-
高频率的事件处理函数中,存在着大量的DOM操作,当浏览器的渲染速度跟不上事件触发频率,容易造成页面卡顿,影响用户体验,甚至会造成 CPU 使用率过高导致页面崩溃。
-
高频率的事件处理函数中,每执行一次事件处理函数时,都需要和服务器通信建立 HTTP 请求,比如页面的搜索功能,那么可能存在短时间内发起了数十条 HTTP 请求的情况,容易消耗服务器资源。
针对这些问题的解决方案,可以采用函数防抖(debounce)和节流(throttle)的方案解决存在的问题,通过防抖和节流可以将多个事件的触发合并成一个,减少事件触发频率。
防抖
设定一个计时器,当事件触发后,一定时间内没有再次触发事件,事件处理函数才会执行一次,如果设定的时间内持续触发一次或多次,计时器又重新开始计时。
立即执行版
事件触发时会立即执行事件处理函数,在设定时间内不触发事件才会执行事件处理函数。
function debounce(fn, delay) {
// 定时器
var timer = null;
return function() {
var that = this;
var args = [].slice.call(arguments);
var callNow = !timer;
if(timer) clearTimeout(timer);
timer = setTimeout(function() {
timer = null;
}, delay)
if(callNow) fn.apply(that, args);
}
}
非立即执行版
事件触发时不会立即执行事件处理函数,必须到了设定时间后才会执行事件处理函数,否则设定时间内触发事件计时器会重新开始技时。
function debounce(fn, delay) {
// 定时器
var timer = null;
return function() {
var that = this;
var args = [].slice.call(arguments);
if(timer) clearTimeout(timer);
timer = setTimeout(function() {
fn.apply(that, args);
}, delay);
}
}
结合版
/**
* @desc 函数防抖
* @param fn Function 事件处理函数
* @param delay Number 设定延时时间
* @param isImmediate Boolean 事件触发时是否立刻执行
*/
function debounce(fn, delay, isImmediate) {
var timer = null;
return function() {
var that = this;
var args = [].slice.call(arguments);
var callNow = !timer && isImmediate;
if(timer) clearTimeout(timer);
// 非立即执行
timer = setTimeout(function() {
timer = null;
if(!isImmediate) fn.apply(that, args);
}, delay);
// 立即执行
if(callNow) fn.apply(that, args);
}
}
// 使用
window.addEventListener('resize', debounce(function() {
console.log('防抖了');
}, 600))
小结
实现函数防抖的关键点:
- 使用 SetTimeout 计时器设定延时的时间,然后根据事件触发频率与延时时间做比较(每两次事件触发的时间间隔是否在设定时间内),控制事件处理函数是否执行。
- 利用闭包的**封装,返回一个新函数在自身的词法作用域外调用,新函数拥有涵盖 debounce 函数内全部作用域的闭包,在闭包中保存了执行状态,在新函数中通过执行状态控制是否在事件触发时执行处理函数。
防抖存在的问题,就是如果在设定时间内持续的触发事件,那么就存在永远无法执行事件处理函数的情况。某些场景下实际上更希望能够在设定的时间段必须执行一次事件处理函数。可以使用节流来实现这样的效果。
节流
将多个时间间隔相近的事件处理函数合并成一个函数执行,并且在指定的时间内执行一次事件处理函数。
函数节流有两种实现方式: 时间戳和定时器。
时间戳
持续事件触发过程中,事件处理函数会立即执行,而后会在每间隔设定时间执行一次。
function throttle(fn, delay) {
var prev = Date.now();
return function() {
var that = this;
var args = [].slice.call(arguments);
var now = Date.now();
if(now - prev >= delay) {
fn.apply(that, args);
prev = now;
}
}
}
定时器
持续事件触发过程中,事件触发后会再到达了设定时间后再执行,而后会在每间隔设定时间执行一次。
function throttle(fn, delay) {
var timer = null;
return function() {
var that = this;
var args = [].slice.call(arguments);
if(!timer) {
timer = setTimeout(function(){
fn.apply(that, args);
timer = null;
}, delay)
}
}
}
结合版
/**
* @desc 函数节流
* @param fn Function 事件处理函数
* @param delay Number 设定执行时间
*/
function throttle(fn, delay) {
var timer = null;
var prev = Date.now();
return function() {
var that = this;
var args = [].slice.call(arguments);
var now = Date.now();
var diff = now - prev;
if(timer) clearTimeout(timer);
if(diff >= delay) {
fn.apply(that, args);
prev = now;
}else {
timer = setTimeout(function() {
fn.apply(that, args);
prev = Date.now();
timer = null;
}, delay)
}
}
}
// 使用
window.addEventListener('resize', debounce(function() {
console.log('节流了');
}, 600))
Underscore 源码
函数防抖的实现
/**
*
* @param {Function} func 回调函数
* @param {Number} wait 设定的延时时间
* @param {Boolean} immediate 是否立即执行
*/
_.debounce = function(func, wait, immediate) {
// 定时器,执行回调函数的返回值
var timeout, result;
var later = function(context, args) {
// 自空定时器
timeout = null;
if (args) result = func.apply(context, args);
};
// 这里使用了类 ES6 rest 参数的实现函数,当感觉有点多余,而且同样的 _.throttle 没使用到,也没影响
// 试了对 _.throttle 使用 restArgs 函数,似乎没什么不同
var debounced = restArgs(function(args) {
// 每次触发的时候先清除之前的定时器任务
if (timeout) clearTimeout(timeout);
// immediate 为 true,表示在触发后会立即执行回调函数,而后在设定的时间内,持续触发,不会执行回调函数,只能在到达了设定的时间后,再次触发才会立即执行回调函数
// immediate 为 false,表示的是触发后需要等到达了设定的时间后,才会执行回调函数,在这个过程中如果发生持续触发,定时器的计时又回重新开始计时
if (immediate) {
// 如果设置立即执行的话,进入到这里
// timeout 定时器没自空的话,表示还在设定时间内,此时触发并不会执行回调函数
var callNow = !timeout;
// 到达设定时间后,自空定时器,这样下次触发会立即执行回调函数
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} else {
// 如果没有设置立即执行的话,进入到这里
timeout = _.delay(later, wait, this, args);
}
return result;
});
// 清除防抖
debounced.cancel = function() {
clearTimeout(timeout);
timeout = null;
};
return debounced;
};
函数节流的实现
/**
* 函数节流
* @param {Function} func 回调函数
* @param {Nubmer} wait 设定的延时时间
* @param {Object} options 配置项
*/
_.throttle = function(func, wait, options) {
// 定时器,执行上下文,回调函数的参数,执行回调函数的返回值
var timeout, context, args, result;
// 存储上一次触发函数的时间戳
var previous = 0;
// 配置参数
// options.leading 为 false,表示禁止第一次触发便立即执行回调函数
// options.trailing 为 false,表示禁止最后一次触发后执行回调函数
if (!options) options = {};
var later = function() {
// console.log(333)
// 根据 leading 配置参数重置前一次触发的时间戳
previous = options.leading === false ? 0 : _.now();
// 自空定时器
timeout = null;
// 执行回调函数
result = func.apply(context, args);
// 清空数据,防止内存泄露
if (!timeout) context = args = null;
};
var throttled = function() {
// 获取当前时间戳
var now = _.now();
// 根据配置的 leading 的值,判断第一次触发是否立即执行回调函数
if (!previous && options.leading === false) previous = now;
// 持续性的两次函数触发(上一次和当前)之间的时间间距和设定的延时时间对比
var remaining = wait - (now - previous);
context = this;
args = arguments;
// 如果持续触发函数之间的时间间距大于设定的延时时间,表示可以立即执行回调函数
// throttle 的时间戳实现方式
if (remaining <= 0 || remaining > wait) {
// console.log(111)
// 如果定时器存在,就先清除定时器任务,立即执行就不需要定时器了
// 避免定时器执行回调函数之前进入到这里,就会执行两次
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
// 重置前一次触发的时间戳
previous = now;
// 执行回调函数
result = func.apply(context, args);
// 清空数据,防止内存泄露,因为变量都存在闭包中,垃圾回收机制无法回收
// 前面已经已经处理过 timeout 定时器了,!timeout 无论如何都是成立的,似乎多余的判断
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
// throttle 的定时器实现方式
// 根据配置的 trailing 的值,判断在最后一次触发后是否还执行回调函数
// 这里的 remaining 值肯定是大于0且小于设定的延时时间的
// 定时器的延时时间为什么不是设定的 wait 值而是计算之后的 remaining 值
// 因为 JavaScript 是单线程的原因,所有的任务不需排队等待执行,JavaScript 任务又分成同步任务和异步任务
// setTimeout 定时器作为异步任务会在到了指定的时间时将事件放入到任务队列中等待执行
// 只有当主线程上的任务都执行完后才会去任务队列中拉去任务执行
// setTimeout 定时器设定的延时时间并不是精准的,因为在执行其他任务的时候可能会耗时
// 等到执行了 setTimeout 定时器的回调函数,再获取当前的时间重置前一次触发的时间戳
// 中间的时间间隔可能就会大于设定的延时时间,也就是 now - previous > wait
// 这样的情况就会导致在高频率的触发事件的时候,总是会立即执行回调函数,而不会进入到这里
// 关于 JavaScript 的单线程,可参考文章: https://github.com/webproblem/Blog/issues/2
// console.log(222)
timeout = setTimeout(later, remaining);
}
return result;
};
// 清除节流
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
};
总结
- 函数防抖: 将一定时间内的事件处理合并成一个,与定时器结合来控制事件处理函数的执行。如果事件触发之间的时间间隔小于设定的延时时间,执行处理函数永远不会执行。
- 函数节流: 一定时间内只会执行一次事件处理,能够保证时间处理函数在设定的时间段内执行一次。假设设置的时间是 2s,事件处理函数就会以每隔 2s 执行一次的频率执行。
参考
Spring boot 和 Spring Cloud版本对应关系
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.