Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usersessiontable.php
1<?php
3
4
9
26class UserSessionTable extends Entity\DataManager
27{
29 public const CONNECTION_NAME = 'user_session';
30
36 public static function getTableName()
37 {
38 return 'b_user_session';
39 }
40
48 public static function getConnectionName()
49 {
50 $pool = Application::getInstance()->getConnectionPool();
51 $isConnectionExists = $pool->getConnection(static::CONNECTION_NAME) !== null;
52 if (!$isConnectionExists)
53 {
54 $pool->cloneConnection(
55 $pool::DEFAULT_CONNECTION_NAME,
56 static::CONNECTION_NAME
57 );
58 }
59
60 return static::CONNECTION_NAME;
61 }
62
68 public static function getMap()
69 {
70 return [
71 new Entity\StringField('SESSION_ID', [
72 'primary' => true,
73 'format' => '#^[0-9a-z\-,]{6,250}$#iD'
74 ]),
75 new Entity\DatetimeField('TIMESTAMP_X', [
76 'default_value' => new Type\DateTime
77 ]),
78 new Entity\TextField('SESSION_DATA', [
79 'default_value' => '',
80 'save_data_modification' => function() {
81 return [
82 function($data) {
83 return base64_encode($data);
84 }
85 ];
86 },
87 'fetch_data_modification' => function() {
88 return [
89 function($data) {
90 return base64_decode($data);
91 }
92 ];
93 },
94 ])
95 ];
96 }
97
105 public static function lock($id, $timeout = 60)
106 {
107 $result = true;
108
109 $pool = Application::getInstance()->getConnectionPool();
110 $pool->useMasterOnly(true);
111
112 $connection = static::getEntity()->getConnection();
113 if ($connection instanceof MysqlCommonConnection)
114 {
115 $result = $connection->lock($id, (int)$timeout);
116 }
117 else
118 {
119 trigger_error(sprintf('SessionTable::lock not supported for connection of type "%s"', get_class($connection)), E_USER_WARNING);
120 }
121
122 $pool->useMasterOnly(false);
123
124 return $result;
125 }
126
133 public static function unlock($id)
134 {
135 $pool = Application::getInstance()->getConnectionPool();
136 $pool->useMasterOnly(true);
137
138 $connection = static::getEntity()->getConnection();
139 if ($connection instanceof MysqlCommonConnection)
140 {
141 $connection->unlock($id);
142 }
143 else
144 {
145 trigger_error(sprintf('SessionTable::unlock not supported for connection of type "%s"', get_class($connection)), E_USER_WARNING);
146 }
147
148 $pool->useMasterOnly(false);
149
150 return true;
151 }
152
159 public static function deleteOlderThan($sec)
160 {
161 $pool = Application::getInstance()->getConnectionPool();
162 $pool->useMasterOnly(true);
163
164 $tableName = static::getTableName();
165 $connection = static::getEntity()->getConnection();
166 $connection->queryExecute(
167 sprintf("delete from {$tableName} where TIMESTAMP_X < %s",
168 $connection->getSqlHelper()->addSecondsToDateTime('-'.$sec))
169 );
170 $pool->useMasterOnly(false);
171 }
172}