简体   繁体   English

SQLite作为会话存储

[英]SQLite as a Session Storage

I wanted to know is it a good practice to use SQLite as a main session storage or at least as a backup session storage with primary memcached? 我想知道将SQLite用作主会话存储或至少作为主要memcached的备份会话存储是一种好习惯吗?

Could you give me some pros and cons? 你能给我一些优点和缺点吗?

(I am building a MVC Framework for education purposes and was thinking of different possibilities and implemetations) (我正在为教育目的构建MVC框架,并考虑了不同的可能性和实现)

SQLite Pros SQLite专业人士

  • faster than file based sessions 比基于文件的会话更快
  • can be distributed where file based sessions are more awkward 可以分布在基于文件的会话更尴尬的地方

SQLite Cons SQLite缺点

  • requires SQLite which creates a dependency and something else to monitor 需要SQLite,它创建一个依赖项和其他要监视的东西
  • harder to implement that native file based sessions 更难实现基于本机文件的会话
  • large applications can quickly kill the sql table by so many read and write requests, fragmentation, index updates, etc especially with almost every page hit hitting that specific table 大型应用程序可以通过如此多的读写请求,碎片,索引更新等快速杀死sql表,尤其是几乎每个页面都会遇到特定的表

Even Better Solution - Memcache 更好的解决方案 - Memcache

Since sessions are usually accessed with every page hit it would make sense to use the fastest mechanism possible without all the overhead of a database layer while still allowing it to work in a distributed system (multiple PHP servers for example). 由于会话通常在每个页面访问时访问,因此在没有数据库层的所有开销的情况下使用最快的机制是有意义的,同时仍然允许它在分布式系统中工作(例如,多个PHP服务器)。

Use Memcache which is well tested with PHP and you can even integrate memcache sessions just by modifying a few php.ini settings or for more fine grained control (or to use other software like redis) you can create your own custom session handler. 使用经过PHP测试的Memcache,您甚至可以通过修改一些php.ini设置或更精细的控制(或使用其他软件如redis)来集成memcache会话,您可以创建自己的自定义会话处理程序。

This has different pros and cons 这有不同的利弊

Memcache Pros Memcache优点

  • Very very fast 非常快
  • Scales well 很好地扩展
  • Easy to implement via PHP.INI 易于通过PHP.INI实现

Memcache Cons Memcache缺点

  • Another service that has the potential to crash and requires monitoring 另一项有可能崩溃并需要监控的服务
  • Uses RAM which is usually a limited resource compaired to HDD space and also requires monitoring 使用RAM通常是与HDD空间相比的有限资源,并且还需要监视

Though you should be using other software that monitor both those things or write a cron job script that checks the memcache service is still running - but thats another question and answer for another day. 虽然您应该使用其他软件监视这些内容或编写一个cron作业脚本来检查内存缓存服务是否仍在运行 - 但这是另一天的另一个问题和答案。 Point is, those cons can be lessened to some degree. 重点是,这些缺点可以在某种程度上减轻。

Further reading on the topics covered 进一步阅读所涵盖的主题

Sessions based on files are a bad idea, because the file will be locked if you dont close the write access to the session ( session_write_close(); ). 基于文件的会话是个坏主意,因为如果不关闭对会话的写访问权(session_write_close();),文件将被锁定。 But why should you limit yourself/theServer when you just have to use sqlite to avoid this problem? 但是,当你必须使用sqlite来避免这个问题时,为什么要限制自己/ theServer?

so sqlite pro: - easy to use (change php.ini config): 所以sqlite pro: - 易于使用(更改php.ini配置):

session.save_handler = sqlite
session.save_path = "/path/sessions.db"
  • faster to load pages (session can now work parallel) 加载页面的速度更快(会话现在可以并行工作)
  • faster with ajax 使用ajax更快
  • build in functionality 内置功能

sqlite con sqlite con

  • slower to write to session 写入会话的速度较慢

i would like to use apc, but then i need to implementation and i'm worried that it could end in security issues... 我想使用apc,但后来我需要实现,我担心它可能会以安全问题结束......

PHP的文件库会话非常好而且快速,使用SQLite存储会话只会增加会话管理的开销。

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

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