简体   繁体   English

IP地址的存储记录(数据库替代)

[英]Storing Record for IP Address (DB Alternative)

I need to store a record for a user per IP address to limit the rate at which a certain IP address can perform an action. 我需要为每个IP地址存储一个用户记录,以限制某个IP地址可以执行操作的速率。

I know that storing the IP in a database would work but I am worried that on a small VPS with limited resources, too much processing power would be taken by the MySQL process. 我知道可以将IP存储在数据库中,但是我担心在资源有限的小型VPS上,MySQL进程会占用太多处理能力。 Is there another way of storing data for an IP? 还有另一种存储IP数据的方法吗?

I thought of a system similar to: 我想到的系统类似于:

/ips/
/ips/127/
/ips/127/0
/ips/127/0/0
/ips/127/0/0/1.txt

Sample Code: 样例代码:

$ip_parts = explode('.', $_SERVER['REMOTE_ADDR']);
$records = intval(file_get_contents('ips/' . implode('/', $ip_parts)));

if($records > 50) {
    echo 'Error - credits used.';
} else {
    // Do something
}

With the 1.txt file containing the needed data. 1.txt文件包含所需的数据。 Would I encounter issues with the number of files or folders causing this method to be slower than a database? 我会遇到文件或文件夹数量问题,导致此方法比数据库慢吗?

What makes you think building your own file-based database scheme is better than software build for the purpose by software engineers with 20 years of experience? 是什么让您认为构建自己的基于文件的数据库方案要比拥有20年经验的软件工程师为实现此目的而进行的软件构建更好? Just use a MySQL database - there's rarely a case where not doing so is better - other than if you actually wish to save files. 仅使用MySQL数据库-很少有这样做的情况比您真正希望保存文件的情况更好。

Check out this question / answer on ServerFault regarding DBMS optimization. 在ServerFault上查看有关DBMS优化的问题/答案 You're trying to reinvent the wheel for no particular reason (unless you won't ever use MySQL on that VPS). 您正在尝试无故重新发明轮子(除非您永远不会在该VPS上使用MySQL)。 And, while on the subject of storing IP addresses in MySQL, take a look at the INET_ATOI() function for MySQL. 并且,在以MySQL存储IP地址为主题的同时,请看一下MySQL的INET_ATOI()函数。

Using a filesystem is not free either and can cause a lot of gray hair on multiple points, so I'm skeptic as to whether it would actually be better than using a database. 使用文件系统也不是免费的,并且可能在多个点上造成很多白发,因此我怀疑它是否真的比使用数据库更好。

That said, whether you end up using files or a database, you would probably want to learn about ip2long and MySQL's equivalent INET_ATON . 也就是说,无论您最终使用文件还是数据库,您都可能想了解ip2long和MySQL的等效INET_ATON Especially if you end up using MySQL, since numeric keys tend to be more efficient than strings. 特别是如果您最终使用MySQL,则由于数字键往往比字符串更有效。

Have you tried sqlite? 您尝试过sqlite吗? It should work well. 它应该运作良好。

http://php.net/manual/en/book.sqlite.php http://php.net/manual/zh/book.sqlite.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM