ecshop换用redis做缓存
<?php
/**
* ecshop SESSION 保存类
* ============================================================================
* 浣跨敤锛涗笉鍏佽?瀵圭▼搴忎唬鐮佷互浠讳綍褰㈠紡浠讳綍鐩?殑鐨勫啀鍙戝竷銆
* ============================================================================
* $Author: liubo $
* $Id: cls_session.php 17217 2011-01-19 06:29:08Z liubo $
*/
if (!defined('IN_ECTOUCH'))
{
die('Hacking attempt');
}
class cls_session
{
var $db = NULL;
var $session_table = '';
var $max_life_time = 1800; // SESSION 保存时间
var $session_name = '';
var $session_id = '';
var $session_expiry = '';
var $session_md5 = '';
var $session_cookie_path = '/';
var $session_cookie_domain = '';
var $session_cookie_secure = false;
var $redis='';
var $_ip = '';
var $_time = 0;
function __construct(&$db, $session_table, $session_data_table, $session_name = 'ECS_ID', $session_id = '')
{
$this->cls_session($db, $session_table, $session_data_table, $session_name, $session_id);
}
function cls_session(&$db, $session_table, $session_data_table, $session_name = 'ECS_ID', $session_id = '')
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
$GLOBALS['_SESSION'] = array();
if (!empty($GLOBALS['cookie_path']))
{
$this->session_cookie_path = $GLOBALS['cookie_path'];
}
else
{
$this->session_cookie_path = '/';
}
if (!empty($GLOBALS['cookie_domain']))
{
$this->session_cookie_domain = $GLOBALS['cookie_domain'];
}
else
{
$this->session_cookie_domain = '';
}
if (!empty($GLOBALS['cookie_secure']))
{
$this->session_cookie_secure = $GLOBALS['cookie_secure'];
}
else
{
$this->session_cookie_secure = false;
}
$this->session_name = $session_name;
$this->session_table = $session_table;
$this->session_data_table = $session_data_table;
$this->db = &$db;
//$this->_ip = real_ip();以下代码解决登陆问题
if(isset($_COOKIE['real_ipd']) && !empty($_COOKIE['real_ipd']))
{
$this->_ip = $_COOKIE['real_ipd'];
}
else
{
$this->_ip = real_ip();
setcookie("real_ipd", $this->_ip, time()+36000, $this->session_cookie_path);
}
if ($session_id == '' && !empty($_COOKIE[$this->session_name]))
{
$this->session_id = $_COOKIE[$this->session_name];
}
else
{
$this->session_id = $session_id;
}
if ($this->session_id)
{
$tmp_session_id = substr($this->session_id, 0, 32);
if ($this->gen_session_key($tmp_session_id) == substr($this->session_id, 32))
{
$this->session_id = $tmp_session_id;
}
else
{
$this->session_id = '';
}
}
$this->_time = time();
if ($this->session_id)
{
$this->load_session();
}
else
{
$this->gen_session_id();
setcookie($this->session_name, $this->session_id . $this->gen_session_key($this->session_id), 0, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);
}
register_shutdown_function(array(&$this, 'close_session'));
}
function gen_session_id()
{
$this->session_id = md5(uniqid(mt_rand(), true));
return $this->insert_session();
}
function gen_session_key($session_id)
{
static $ip = '';
if ($ip == '')
{
$ip = substr($this->_ip, 0, strrpos($this->_ip, '.'));
}
return sprintf('%08x', crc32(ROOT_PATH . $ip . $session_id));
}
function insert_session()
{
$sess_array=array();
//$sess_array["expiry"]=$this->_time;
$sess_array["ip"]=$this->_ip;
$sess_array["data"]='a:0:{}';
$sess=serialize($sess_array);
$return=$this->redis->set($this->session_id, $sess);
$this->redis->expire($this->session_id,$max_life_time);
return $return;
// return $this->db->query('INSERT INTO ' . $this->session_table . " (sesskey, expiry, ip, data) VALUES ('" . $this->session_id . "', '". $this->_time ."', '". $this->_ip ."', 'a:0:{}')");
}
function load_session()
{
// $session = $this->db->getRow('SELECT userid, adminid, user_name, user_rank, discount, email, data, expiry FROM ' . $this->session_table . " WHERE sesskey = '" . $this->session_id . "'");
$session=$this->redis->get($this->session_id);
if ($session)
{
$this->insert_session();
$this->session_expiry = 0;
$this->session_md5 = '40cd750bba9870f18aada2478b24840a';
$GLOBALS['_SESSION'] = array();
}
else
{
$session=unserialize($session);
if (!empty($session['data']))
{
$this->session_expiry =$this->max_life_time;
$this->redis->expire($this->session_id,$max_life_time);
$this->session_md5 = md5($session['data']);
$GLOBALS['_SESSION'] = unserialize($session['data']);
$GLOBALS['_SESSION']['user_id'] = $session['userid'];
$GLOBALS['_SESSION']['admin_id'] = $session['adminid'];
$GLOBALS['_SESSION']['user_name'] = $session['user_name'];
$GLOBALS['_SESSION']['user_rank'] = $session['user_rank'];
$GLOBALS['_SESSION']['discount'] = $session['discount'];
$GLOBALS['_SESSION']['email'] = $session['email'];
}
}
}
function update_session()
{
$adminid = !empty($GLOBALS['_SESSION']['admin_id']) ? intval($GLOBALS['_SESSION']['admin_id']) : 0;
$userid = !empty($GLOBALS['_SESSION']['user_id']) ? intval($GLOBALS['_SESSION']['user_id']) : 0;
$user_name = !empty($GLOBALS['_SESSION']['user_name']) ? trim($GLOBALS['_SESSION']['user_name']) : 0;
$user_rank = !empty($GLOBALS['_SESSION']['user_rank']) ? intval($GLOBALS['_SESSION']['user_rank']) : 0;
$discount = !empty($GLOBALS['_SESSION']['discount']) ? round($GLOBALS['_SESSION']['discount'], 2) : 0;
$email = !empty($GLOBALS['_SESSION']['email']) ? trim($GLOBALS['_SESSION']['email']) : 0;
unset($GLOBALS['_SESSION']['admin_id']);
unset($GLOBALS['_SESSION']['user_id']);
unset($GLOBALS['_SESSION']['user_name']);
unset($GLOBALS['_SESSION']['user_rank']);
unset($GLOBALS['_SESSION']['discount']);
unset($GLOBALS['_SESSION']['email']);
$data = serialize($GLOBALS['_SESSION']);
// $this->_time = time();
// if ($this->session_md5 == md5($data) && $this->_time < $this->session_expiry + 10)
// {
// return true;
// }
$data = addslashes($data);
// if (isset($data{255}))
// {
// $this->db->autoReplace($this->session_data_table, array('sesskey' => $this->session_id, 'expiry' => $this->_time, 'data' => $data), array('expiry' => $this->_time,'data' => $data));
// $data = '';
// }
$sess=array();
//$sess["expiry"]=$this->_time;
$sess["ip"]=$this->_ip;
$sess["userid"]=$userid;
$sess["user_name"]=$user_name;
$sess["user_rank"]=$user_rank;
$sess["discount"]=$discount;
$sess["email"]=$email;
$sess["data"]=$data;
$session=serialize($sess);
$return=$this->redis->set($this->session_id, $session);
$this->redis->expire($this->session_id,$this->max_life_time);
return $return;
// return $this->db->query('UPDATE ' . $this->session_table . " SET expiry = '" . $this->_time . "', ip = '" . $this->_ip . "', userid = '" . $userid . "', adminid = '" . $adminid . "', user_name='" . $user_name . "', user_rank='" . $user_rank . "', discount='" . $discount . "', email='" . $email . "', data = '$data' WHERE sesskey = '" . $this->session_id . "' LIMIT 1");
}
function close_session()
{
$this->update_session();
/* 闅忔満瀵 sessions_data 鐨勫簱杩涜?鍒犻櫎鎿嶄綔 */
// if (mt_rand(0, 2) == 2)
// {
// $this->db->query('DELETE FROM ' . $this->session_data_table . ' WHERE expiry < ' . ($this->_time - $this->max_life_time));
// }
// if ((time() % 2) == 0)
// {
// return $this->redis->del()
// // return $this->db->query('DELETE FROM ' . $this->session_table . ' WHERE expiry < ' . ($this->_time - $this->max_life_time));
// }
return true;
}
function delete_spec_admin_session($adminid)
{
// if (!empty($GLOBALS['_SESSION']['admin_id']) && $adminid)
// {
// return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE adminid = '$adminid'");
// }
// else
// {
return true;
// }
}
function destroy_session()
{
$GLOBALS['_SESSION'] = array();
setcookie($this->session_name, $this->session_id, 1, $this->session_cookie_path, $this->session_cookie_domain, $this->session_cookie_secure);
/* weifenxiao 删除购物车 */
if (!empty($GLOBALS['ecs']))
{
$this->db->query('DELETE FROM ' . $GLOBALS['ecs']->table('cart') . " WHERE session_id = '$this->session_id'");
}
/* weifenxiao 鑷?畾涔夋墽琛岄儴鍒 */
// $this->db->query('DELETE FROM ' . $this->session_data_table . " WHERE sesskey = '" . $this->session_id . "' LIMIT 1");
if (!$this->redis->exists($this->session_id)) {
return true;
}
return $this->redis->del($this->session_id);
// return $this->db->query('DELETE FROM ' . $this->session_table . " WHERE sesskey = '" . $this->session_id . "' LIMIT 1");
}
function get_session_id()
{
return $this->session_id;
}
function get_users_count()
{
return count($this->redis->key("*"));
}
}
?>
ecshop换用redis做缓存的更多相关文章
- 如何用redis做缓存
redis缓存 在互联网应用中经常需要用redis来缓存热点数据. redis数据在内存,可以保证数据读取的高效,接近每秒数十万次的吞吐量 减少下层持久层数据库读取压力,像mongodb,每秒近千次读 ...
- spring+redis的集成,redis做缓存
1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...
- SpringBoot之Mybatis操作中使用Redis做缓存
上一博客学习了SpringBoot集成Redis,今天这篇博客学习下Mybatis操作中使用Redis做缓存.这里其实主要学习几个注解:@CachePut.@Cacheable.@CacheEvict ...
- spring-boot集成mybatis,用redis做缓存
网上有很多例子了,执行源码起码有3个,都是各种各样的小问题. 现在做了个小demo,实现spring-boot 用redis做缓存的实例,简单记录下思路,分享下源码. 缓存的实现,分担了数据库的压力, ...
- django使用redis做缓存
Django 使用 Redis 做缓存 django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { ...
- Spring Boot 2整合Redis做缓存
既然是要用Redis做缓存,自然少不了安装了.但是本文主要讲Spring Boot与Redis整合.安装教程请另行百度! 1.首先是我们的Redis配置类 package com.tyc; impor ...
- springboot2.0+redis实现消息队列+redis做缓存+mysql
本博客仅供参考,本人实现没有问题. 1.环境 先安装redis.mysql 2.springboot2.0的项目搭建(请自行完成),本人是maven项目,因此只需配置,获取相应的jar包,配置贴出. ...
- spring使用redis做缓存
缓存 什么是缓存? 在高并发下,为了提高访问的性能,需要将数据库中 一些经常展现和不会频繁变更的数据,存放在存取速率更快的内存中.这样可以 降低数据的获取时间,带来更好的体验 减轻数据库的压力 缓存适 ...
- 简单封装Redis做缓存
基于Redis封装一个简单的Python缓存模块 0. Docker Redis安装 参考: Get Docker CE for CentOS Docker 安装 Redis 安装Docker时错误s ...
随机推荐
- html 绘图渐变和图片填充
包括线性渐变和径向渐变 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&qu ...
- Kefa and Park
#include<bits/stdc++.h> #define max 100005 using namespace std; int cats[max]; vector<int&g ...
- 模拟 ACdream 1196 KIDx's Pagination
题目传送门 /* 简单模拟:考虑边界的情况输出的是不一样的,还有思维好,代码能短很多 */ #include <cstdio> #include <iostream> #inc ...
- Python 安装mssql (Ubuntu)
1. Python.h:没有那个文件或目录 apt-get install python-dev 2.sqlfront.h:没有那个文件或目录 apt-get install freetds-dev
- Ubuntu 15.10安装elementary desktop
elementaryOS的风格类似于macos,基于Ubuntu改造,个人比较喜欢其界面理念,简单清晰.因此下载了elementaryOS Freya使用,使用过程中,各个软件版本的升级比较落后,比如 ...
- Stackoverflow架构
Stackoverflow用的是.net开发的,用的缓存是Redis,Stackoverflow架构的演讲地址是:http://www.infoq.com/cn/presentations/archi ...
- HashTable的典型用法以及参考实例
Get-ADComputer -Identity "cnhzpd-f7sc83x" | select -property @{name="computername&quo ...
- memcached与spring
key的生成规则 update 与 query 的参数不一样,如何让其生成一样的key 列表缓存如何定义key及失效 最近同事推荐了一个开源项目:Simple-Spring-Memcached(简称s ...
- Scrum会议3
组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.18 已完成内容: 1.GUI布局设计 2.通过在网上大量阅览代码,大体了解连连看游戏制作 ...
- OpenCV学习笔记——图像的腐蚀与膨胀
顺便又复习了一下cvcopy如何进行图像拼接(最近觉得打开多幅图像分别看不如缩小掉放拼接到一幅图像上对比来的好) 首先把拼接的目标图像设置兴趣区域ROI,比如我有一个total,要把a.b.c分别从左 ...