<commit_before><?php

namespace Friendica\Core\Session;

use Friendica\BaseObject;
use Friendica\Core\Cache;
use Friendica\Core\Session;
use SessionHandlerInterface;

require_once 'boot.php';
require_once 'include/text.php';

/**
 * SessionHandler using Friendica Cache
 *
 * @author <NAME> <<EMAIL>>
 */
class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
{
	public function open($save_path, $session_name)
	{
		return true;
	}

	public function read($session_id)
	{
		if (!x($session_id)) {
			return '';
		}

		$data = Cache::get('session:' . $session_id);
		if (!empty($data)) {
			Session::$exists = true;
			return $data;
		}
		logger("no data for session $session_id", LOGGER_TRACE);
		return '';
	}

	/**
	 * @brief Standard PHP session write callback
	 *
	 * This callback updates the stored session data and/or the expiration depending
	 * on the case. Uses the Session::expire for existing session, 5 minutes
	 * for newly created session.
	 *
	 * @param  string $session_id   Session ID with format: [a-z0-9]{26}
	 * @param  string $session_data Serialized session data
	 * @return boolean Returns false if parameters are missing, true otherwise
	 */
	public function write($session_id, $session_data)
	{
		if (!$session_id) {
			return false;
		}

		if (!$session_data) {
			return true;
		}

		Cache::set('session:' . $session_id, $session_data, Session::$expire);

		return true;
	}

	public function close()
	{
		return true;
	}

	public function destroy($id)
	{
		Cache::delete('session:' . $id);
		return true;
	}

	public function gc($maxlifetime)
	{
		return true;
	}
}
<commit_msg>Improve CacheSessionhandler<commit_after><?php

namespace Friendica\Core\Session;

use Friendica\BaseObject;
use Friendica\Core\Cache;
use Friendica\Core\Session;
use SessionHandlerInterface;

require_once 'boot.php';
require_once 'include/text.php';

/**
 * SessionHandler using Friendica Cache
 *
 * @author <NAME> <<EMAIL>>
 */
class CacheSessionHandler extends BaseObject implements SessionHandlerInterface
{
	public function open($save_path, $session_name)
	{
		return true;
	}

	public function read($session_id)
	{
		if (empty($session_id)) {
			return '';
		}

		$data = Cache::get('session:' . $session_id);
		if (!empty($data)) {
			Session::$exists = true;
			return $data;
		}
		logger("no data for session $session_id", LOGGER_TRACE);
		return '';
	}

	/**
	 * @brief Standard PHP session write callback
	 *
	 * This callback updates the stored session data and/or the expiration depending
	 * on the case. Uses the Session::expire for existing session, 5 minutes
	 * for newly created session.
	 *
	 * @param  string $session_id   Session ID with format: [a-z0-9]{26}
	 * @param  string $session_data Serialized session data
	 * @return boolean Returns false if parameters are missing, true otherwise
	 */
	public function write($session_id, $session_data)
	{
		if (!$session_id) {
			return false;
		}

		if (!$session_data) {
			return true;
		}

		$return = Cache::set('session:' . $session_id, $session_data, Session::$expire);

		return $return;
	}

	public function close()
	{
		return true;
	}

	public function destroy($id)
	{
		$return = Cache::delete('session:' . $id);

		return $return;
	}

	public function gc($maxlifetime)
	{
		return true;
	}
}

{'fraction_non_alphanumeric': 0.12439467312348668, 'fraction_numerical': 0.003026634382566586, 'mean_word_length': 4.030441400304414, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382758', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1040, 'n_words': 297}