您现在的位置是:首页 > cms教程 > Ecshop商城教程Ecshop商城教程
ecshop改造读写分离的实现方法
池湃邦2025-01-06Ecshop商城教程已有人查阅
导读前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离。以下代码仅供学习参考,不成熟的地方,还需完善。
前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离。以下代码仅供学习参考,不成熟的地方,还需完善。
config.php
config.php
<?php
$db_name = "ecshop";
$prefix = "ecs_";
$timezone = "Europe/Berlin";
$cookie_path = "/";
$cookie_domain = "";
$session = "1440";
$_config = array();
//数据库主服务器设置, 支持多组服务器设置, 当设置多组服务器时, 则会随机使用某个服务器
$_config['master'][1]['dbhost'] = "192.168.2.175:3306";
$_config['master'][1]['dbname'] = "ecshop";
$_config['master'][1]['dbuser'] = "dragon";
$_config['master'][1]['dbpw'] = "loong";
/*
*$_config['master'][2]['dbhost'] = "";
*...
*/
//数据库从服务器设置( slave, 只读 ), 支持多组服务器设置, 当设置多组服务器时, 系统每次随机使用
$_config['slave'][1]['dbhost'] = "192.168.2.176:3306";
$_config['slave'][1]['dbname'] = "ecshop";
$_config['slave'][1]['dbuser'] = "ivan";
$_config['slave'][1]['dbpw'] = "loong";
$_config['slave'][2]['dbhost'] = "192.168.2.177:3306";
$_config['slave'][2]['dbname'] = "ecshop";
$_config['slave'][2]['dbuser'] = "ivan";
$_config['slave'][2]['dbpw'] = "loong";
define('EC_CHARSET','utf-8');
define('ADMIN_PATH','admin');
define('AUTH_KEY', 'this is a key');
define('OLD_AUTH_KEY', '');
define('API_TIME', '');
?>
初始化数据连接类
/* 初始化数据库类
* 如果配置了从服务器,则初始化从库类
*/
if(count($_config['slave'])) {
require(ROOT_PATH . 'includes/cls_mysql_slave.php');
$db = new cls_mysql_slave($_config);
}else{
require(ROOT_PATH . 'includes/cls_mysql.php');
$db = new cls_mysql($_config);
}
增加cls_mysql_slave.php从库类
<?php
require(ROOT_PATH . 'includes/cls_mysql.php');
class cls_mysql_slave extends cls_mysql
{
var $slaveid = null;
function set_config($config){
if(!empty($this->config['slave'])) {
$this->slaveid = array_rand($this->config['slave']);
}
parent::set_config($config);
}
/* 随机分配从库连接 */
function set_slave_config() {
$this->settings = $this->config['slave'][$this->slaveid];
$this->settings['charset'] = $this->config['charset'];
$this->settings['pconnect'] = $this->config['pconnect'];
}
function slave_connect() {
$this->set_slave_config();
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname);
}
function query($sql, $type = '') {
// 如果执行查询操作,则执行从库连接
if($this->slaveid && strtoupper(substr($sql, 0 , 6)) == 'SELECT') {
$this->slave_connect();
}else{
parent::set_config($this->config);
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname);
}
return parent::query($sql, $type);
}
/* 删除失败连接*/
function del_error_link(){
unset($this->config['slave'][$this->slaveid]);
$this->set_config($this->config);
$this->set_slave_config();
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname);
}
}
cls_mysql.php文件类修改
<?php
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
class cls_mysql
{
var $link_id = NULL;
var $settings = array();
var $queryCount = 0;
var $linkCount = 0;
var $queryTime = '';
var $queryLog = array();
var $max_cache_time = 300; // 较大的缓存时间,以秒为单位
var $cache_data_dir = 'temp/query_caches/';
var $root_path = '';
var $error_message = array();
var $platform = '';
var $version = '';
var $dbhash = '';
var $starttime = 0;
var $timeline = 0;
var $timezone = 0;
var $mysql_config_cache_file_time = 0;
var $mysql_disable_cache_tables = array(); // 不允许被缓存的表,遇到将不会进行缓存
var $config = array();
function __construct($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
$this->cls_mysql($config, $charset, $pconnect, $quiet);
}
function cls_mysql($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
if(!empty($config)) {
$config['charset'] = $charset;
$config['pconnect'] = $pconnect;
$this->config = $config;
}
if (defined('EC_CHARSET'))
{
$charset = strtolower(str_replace('-', '', EC_CHARSET));
}
if (defined('ROOT_PATH') && !$this->root_path)
{
$this->root_path = ROOT_PATH;
}
$this->set_config($this->config);
if ($quiet)
{
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
}
}
//随机分配数据库连接
function set_config($config) {
$sid = array_rand($config['master']);
$settings = $config['master'][$sid];
$settings['sid'] = $sid;
$settings['charset'] = $this->config['charset'];
$settings['pconnect'] = $this->config['pconnect'];
$this->settings = $settings;
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
if ($pconnect)
{
if (!($this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this->ErrorMsg("Can't pConnect MySQL Server!");
}
return false;
}
}
else
{
if (PHP_VERSION >= '4.2')
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
}
if (!$this->link_id)
{
if (!$quiet)
{
//连接超过10次,中断连接,抛出错误
if($this->linkCount>9){
$this->ErrorMsg("Can't Connect MySQL Server!");
}
$this->linkCount++;
$this->del_error_link();
}
return false;
}
}
$this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this->version = mysql_get_server_info($this->link_id);
/* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
if ($this->version > '4.1')
{
if ($charset != 'latin1')
{
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
}
if ($this->version > '5.0.1')
{
mysql_query("SET sql_mode=''", $this->link_id);
}
}
$sqlcache_config_file = $this->root_path . $this->cache_data_dir . 'sqlcache_config_file_' . $this->dbhash . '.php';
@include($sqlcache_config_file);
$this->starttime = time();
if ($this->max_cache_time && $this->starttime > $this->mysql_config_cache_file_time + $this->max_cache_time)
{
if ($dbhost != '.')
{
$result = mysql_query("SHOW VARIABLES LIKE 'basedir'", $this->link_id);
$row = mysql_fetch_assoc($result);
if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "\\")
{
$this->platform = 'WINDOWS';
}
else
{
$this->platform = 'OTHER';
}
}
else
{
$this->platform = 'WINDOWS';
}
if ($this->platform == 'OTHER' &&
($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') ||
(PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC'))
{
$result = mysql_query("SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone", $this->link_id);
$row = mysql_fetch_assoc($result);
if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306')
{
$this->timeline = $this->starttime - $row['timeline'];
}
if (PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC')
{
$this->timezone = $this->starttime - $row['timezone'];
}
}
$content = '<' . "?php\r\n" .
'$this->mysql_config_cache_file_time = ' . $this->starttime . ";\r\n" .
'$this->timeline = ' . $this->timeline . ";\r\n" .
'$this->timezone = ' . $this->timezone . ";\r\n" .
'$this->platform = ' . "'" . $this->platform . "';\r\n?" . '>';
@file_put_contents($sqlcache_config_file, $content);
}
/* 选择数据库 */
if ($dbname)
{
if (mysql_select_db($dbname, $this->link_id) === false )
{
if (!$quiet)
{
$this->ErrorMsg("Can't select MySQL database!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
......
/* 删除失败连接*/
function del_error_link(){
unset($this->config['master'][$this->settings['sid']]);
$this->set_config($this->config);
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname);
}
}
本文标签:
很赞哦! ()
下一篇:ecshop模板开发问题总结
相关教程
- discuz的ecshop的伪静态规则apache+nginx
- ecshop整合discuz的方法
- discuz和ecshop截取字符串函数介绍
- ECShop2.7.2整合Discuz 6.0论坛的步骤方法
- Ecshop和Discuz开源产品有哪些局限性
- ecshop怎么和discuz整合
- phpcmsv9和ecshop2.3.7以及discuzx3.0整合方法
- Nginx常用Rewrite(伪静态规则)WordPress/PHPCMS/ECSHOP/ShopEX/S
- ecshop收货地址199/198号段提示手机号格式不正确!
- ecshop调用商品属性的方法实例
- ecshop支付插件开发教程
- ecshop商品怎么增加新字段
图文教程
ecshop首页发货显示省市区地址信息的方法
问题描述:默认的ECSHOP系统,首页发货查询只显示订单号和发货单号,如何将送货地址中的“省份+城市+区县” 都显示出来呢?
ecshop2.x 3.0执行漏洞分析
0×00 前言ECShop是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店。2.x版本跟3.0版本存在代码执行漏洞。0×01 漏洞原理 ECShop 没有对 $GLOBAL[‘_SERVER’][
ecshop页面底部的在线人数怎么修改
建站之初,网站的在线人数肯定不会很多,那么我们可以稍微修改下ECSHOP的程序,让页面底部显示的在线人数翻几番。
ecshop商品页显示优惠节省钱数的方法
在ECSHOP商品详情页面增加一个“本店价”相对于“市场价”的折扣比率或者是节省的钱数,岂不是一个很酷的效果,而且让用户看着更明白,对购买者更有吸引力。
相关源码
-
自适应APP应用程序介绍推广落地页pbootcms网站源码下载移动应用开发商设计的营销型落地页模板,基于PbootCMS内核深度开发采用前沿响应式架构,无缝适配手机端操作习惯与PC端展示需求。查看源码 -
(自适应响应式)工业机床工程农业机械设备网站源码下载框架适用于工程机械、机床设备等工业领域。通过模块调整可快速转型为农业机械、物流设备展示系统。预留7种工业产品展示模板。查看源码 -
(自适应)WordPress主题SEO自媒体博客资讯模板RabbitV2.0Rabbit v2.0主题专注于网站搜索引擎优化需求,为博客、自媒体及资讯类网站提供专业的SEO技术解决方案。该主题从架构设计到功能实现均围绕搜索引擎优化理念展开。查看源码 -
快递物流公司pbootcms网站模板html响应式自适应源码下载基于HTML5+CSS3前沿技术开发,实现PC、平板、手机多端自适应。采用弹性布局与媒体查询技术,确保不同设备均有流畅视觉体验,企业形象统一。查看源码 -
帝国cms自适应古诗词古籍名句网站整站带数据基于帝国CMS打造的专业古诗词文化网站模板,专注于古典文学内容的展示与传播。模板设计蕴含传统文化韵味,支持诗词鉴赏、名句赏析、古籍整理等特色功能,为诗词爱好者提供优质的在线阅读体验。查看源码 -
HTML5响应式健身俱乐部pbootcms网站模板下载为健身俱乐部、瑜伽中心及运动场馆设计的响应式网站模板,采用PbootCMS内核开发,支持一键替换图文适配健身器材销售、瑜伽工作室等多类型运动健康产业。查看源码
| 分享笔记 (共有 篇笔记) |
