简体   繁体   English

在数据库中按字节或文本类型列更好地存储会话数据

[英]Storing session data better bytea or text type column in database

I would store sessions data in database, but im not sure whitch type of column is better to use (i will store serialized objects and arrays). 我会将会话数据存储在数据库中,但是我不确定使用哪种类型的列更好(我将存储序列化的对象和数组)。 I think better way is use BYTEA cause when i tried some days ago store object with namespace the pdo of php lose one`s head (slashes from namespace). 我认为更好的方法是使用BYTEA原因,当我几天前尝试使用命名空间存储对象时,php的pdo失去了头(从命名空间斜杠)。 To slove a problem it turned store object in BYTEA colum and tell php use type PDO::PARAM_LOB when bind values. 为了解决问题,它在BYTEA列中存储了对象,并在绑定值时告诉php使用类型PDO :: PARAM_LOB。

  $this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB);

but this param work only on BYTEA column (Postgresql). 但是此参数仅适用于BYTEA列(Postgresql)。 So it is good idea choose BYTEA column type instead TEXT ??? 因此,最好选择BYTEA列类型而不是TEXT ??? Is something interested to know when use this columns type?? 有兴趣知道何时使用此列类型吗?

I will grateful for solution. 我将不胜感激。

UPDATE: 更新:

i would implements my session handler system using session_set_save_handler function and SessionHandlerInterface. 我将使用session_set_save_handler函数和SessionHandlerInterface实现我的会话处理程序系统。 It look something like: 它看起来像:

class SessionHandler{

   public function open($save_path, $session_name) {// some code}

   public function close() {//some code}

   public function read($id) {// some code}

   public function write($id, $data) {
   // Here i would implement mechanism to store object into database using php pdo. 
   // Here variable $data is already serialized by php session mechanism and ready to put into database. 
   // But i have unpleasantly experience storing serialized object 
   // using pdo client with namespaces thought bindParam function.

   // This experience is:
   // The serialized object (string) with namespace was cut at half, 
   // when i tried used PDO::PARAM_STR as argument in $sth->bindValue() (TEXT column i database). 
   // When to the same operation i used PDO::PARAM_LOB (BYTEA column in database) it was stored all fine.
   }

   public function destroy($id) {//some code}

   public function gc($maxlifetime) {// some code}

}

$hnadler = new SessionHandler()
session_set_save_handler($handler, true);

And after this experience im not sure which coulm use. 而在经历之后,我不确定使用哪个库仑。

UPDATE: 更新:

Thank you for cleaver answer Craig Ringer: After your post i decide to go check what exactly is written in php manual about serializing objects: 感谢cleaver的回答克雷格·林格(Craig Ringer):在您发表文章后,我决定去检查一下php手册中有关对象序列化的确切内容:

http://www.php.net/manual/en/function.serialize.php http://www.php.net/manual/zh/function.serialize.php

serialize function 序列化功能

  Returns a string containing a byte-stream representation of value 
  that can be stored anywhere.

  Note that this is a binary string which may include null bytes, 
  and needs to be stored and handled as such. For example, serialize() 
  output should generally be stored in a BLOB field in a database, 
  rather than a CHAR or TEXT field.

So your suggestion to store objects in BYTEA type column instead TEXT is good. 因此,您建议将对象存储在BYTEA类型列而不是TEXT中是好的。

If you are storing serialized application data, bytea is the appropriate choice. 如果要存储序列化的应用程序数据,那么bytea是合适的选择。 It's just bytes, it's meaningless to anything except the application that supplied it, and it isn't text , in the sense that it isn't meangful characters. 它只是字节,对于提供它的应用程序来说,除了它不是文本 ,从某种意义上说,它不是恶意字符,对任何东西都没有意义。

So for data modelling reasons, bytea is what you should be using. 因此,出于数据建模的原因,应使用bytea

Also, if the data may contain null bytes, you must use bytea , because text cannot hold null bytes, and encoding your data as (eg) base64 to work around that is just inefficient and horrible. 另外,如果数据可能包含空字节,则必须使用bytea ,因为text不能包含空字节,并且将数据编码为(例如)base64以解决该问题是效率低下且令人恐惧的。

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

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