Mercurial > prosody-hg
comparison util/ip.lua @ 8436:ab9ddfb03d4d
util.ip: Cache return values of all methods in one place
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 01 Dec 2017 02:25:25 +0100 |
| parents | 47195f035d2f |
| children | 021129f7b0a3 |
comparison
equal
deleted
inserted
replaced
| 8435:47195f035d2f | 8436:ab9ddfb03d4d |
|---|---|
| 10 | 10 |
| 11 local ip_methods = {}; | 11 local ip_methods = {}; |
| 12 | 12 |
| 13 local ip_mt = { | 13 local ip_mt = { |
| 14 __index = function (ip, key) | 14 __index = function (ip, key) |
| 15 return ip_methods[key](ip); | 15 local method = ip_methods[key]; |
| 16 if not method then return nil; end | |
| 17 local ret = method(ip); | |
| 18 ip[key] = ret; | |
| 19 return ret; | |
| 16 end, | 20 end, |
| 17 __tostring = function (ip) return ip.addr; end, | 21 __tostring = function (ip) return ip.addr; end, |
| 18 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end | 22 __eq = function (ipA, ipB) return ipA.addr == ipB.addr; end |
| 19 }; | 23 }; |
| 20 | 24 |
