2℃
刚才写一个lua上实现php中的strpos()函数,下面在来个strrpos()函数,查找某个字符串在指定字符串最后一次出现的位置,下面我们还是简单写一下函数,代码如下:
function strrpos (str, f)
if str ~= nil and f ~= nil then
local t = true
local offset = 1
local result = nil
while (t)
do
local tmp = string.find(str, f, offset)
if tmp ~= nil then
offset = offset + 1
...
lua, php, string, strrpos阅读全文
Lua中实现php的strpos()函数 [原创]已关闭评论
在来写一个lua中实现php的strpos()函数,查找某个字符串在指定字符串首次出现的位置,其实lua中也为我们提供了这样的函数使用string.find()即可获得,下面我们还是简单写一个函数,代码如下:
function strpos (str, f)
if str ~= nil and f ~= nil then
return (string.find(str, f))
else
return nil
end
end
测试如下图所示:
lua, php, string.find, strpos阅读全文
16℃
下载服务器时常被人盗链,时间久了导致服务器大量资源浪费,由于服务器使用nginx做为web服务器。nginx的防盗链方法有很多,可以使用现成的防盗链模块nginx-accesskey-2.0.3,编译ningx时添加此模块即可。
由于服务其他业务需要,所以nginx编译了lua模块,所以就想通过lua来实现下载服务器的防盗链功能,这样就可以免去了accesskey模块。原理就是生成经过处理过的下载链接,然后下载服务器在处理这个下载链接,成功则下载,失败则阻止。下面就是详细的配置实例:(其中要编译nginx添加lua模块支持,可参考:http://...
lua, nginx, ngx_lua阅读全文
lua实现split字符串分隔 [原创]已关闭评论
使用ngx_memc和ngx_srcache模块构建高效透明的缓存机制已关闭评论
为了提高性能,几乎所有互联网应用都有缓存机制,其中Memcache是使用非常广泛的一个分布式缓存系统。众所周知,LAMP是非常经典的Web架构方式,但是随着Nginx的成熟,越来越多的系统开始转型为LNMP(Linux+Nginx+MySQL+PHP with fpm),这是因为Nginx采用基于事件机制的I/O多路复用思想设计,在高并发情况下其性能远远优于默认采用prefork模式的Apache,另外,相对于Apache,Nginx更轻量,同时拥有大量优秀的扩展模块,使得在Nginx上可以实现一些美妙的功能。
传统上,PHP中使用memcache的方法是使用php-memcache或...
lamp, lnmp, lua, memc, memcache, nginx, srcache阅读全文
3℃
lua 中pairs 和 ipairs的区别
ipairs (t)
Returns three values: an iterator function, the table t, and 0, so that the construction
for i,v in ipairs(t) do body end
will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table.
pairs (t)
Returns three values: the next function, the table t, and nil, so that the construction
for k,v in pairs(t) do body end
will iterate over all key–value pairs of table t.
See function next for the caveat...
ipairs, lua, pairs阅读全文
3℃
之前写了个类似php的var_dump()函数,下面在来一个print_r()函数,代码如下:
function pr (t, name, indent)
local tableList = {}
function table_r (t, name, indent, full)
local id = not full and name or type(name)~="number" and tostring(name) or '['..name..']'
local tag = indent .. id .. ' = '
local out = {} -- result
if type(t) == "table" then
if tableList[t] ~= nil then
table.insert...
lua, php, print_r阅读全文
lua实现php的var_dump()函数功能 [原创]已关闭评论
习惯了php中的var_dump()函数,而如今写lua的时候总习惯使用var_dump()函数,于是就自己动手写了一个类似功能的var_dump()函数。
function var_dump(data, max_level, prefix)
if type(prefix) ~= "string" then
prefix = ""
end
if type(data) ~= "table" then
print(prefix .. tostring(data))
else
print(data)
if max_level ~= 0 then
local prefix_next = prefix .. " "
print(prefix .. "{") ...
lua, php, var_dump阅读全文
Lua5.1基本函数库介绍已关闭评论
Lua5.1基本函数库介绍
assert (v [, message])
功能:相当于C的断言,
参数:
v:当表达式v为nil或false将触发错误,
message:发生错误时返回的信息,默认为"assertion failed!"
collectgarbage (opt [, arg])
功能:是垃圾收集器的通用接口,用于操作垃圾收集器
参数:
opt:操作方法标志
"stop": 停止垃圾收集器
"restart": 重启垃圾收集器
"collect": 执行一次全垃圾收集循环
"count": 返回当前Lua中使用的内存量(以KB为单位)
"step": 单步执行一个垃圾收集. 步长 "size" 由参数arg指定 (大型的值需要多步才能...
assert, dofile, error, lua, tonumber阅读全文
Lua5.1数学库函数参考已关闭评论
Lua5.1中数学库的所有函数如下表:
math.pi 为圆周率常量 = 3.14159265358979323846
abs
取绝对值
math.abs(-15)
15
acos
反余弦函数
math.acos(0.5)
1.04719755
asin
反正弦函数
math.asin(0.5)
0.52359877
atan2
x / y的反正切值
math.atan2(90.0, 45.0)
1.10714871
atan
反正切函数
math.atan(0.5)
0.463647609
ceil
不小于x的最大整数
math.ceil(5.8)
6
cosh
双曲线余弦函数
math.cosh(0.5)
1.276259652
cos
余弦函数
math.cos(0.5)
0.87758256
deg
弧度转角度
math.deg(math.pi)
180
ex...
lua, math阅读全文