From 3ebd2f4b1e5f9f162db10803663308fedd97e812 Mon Sep 17 00:00:00 2001 From: Vimal Joseph Date: Wed, 19 Nov 2014 02:08:46 +0530 Subject: [PATCH] Improved caching system. Improved the structure of caching system. Fixed minor bugs in the caching system. --- CRM/Wci/BAO/WidgetCache.php | 48 ++++++++-------- CRM/Wci/DAO/WidgetCache.php | 16 ++---- CRM/Wci/Form/CreateWidget.php | 10 +++- CRM/Wci/Form/WCISettings.php | 27 ++------- CRM/Wci/Upgrader.php | 73 ++----------------------- CRM/Wci/WidgetCode.php | 29 +++++----- extern/embed.php | 18 +++--- info.xml | 4 +- settings/wci.setting.php | 20 +------ sql/install.sql | 7 +-- templates/CRM/Wci/Form/CreateWidget.tpl | 24 +------- templates/CRM/Wci/Form/NewEmbedCode.tpl | 11 +--- 12 files changed, 75 insertions(+), 212 deletions(-) diff --git a/CRM/Wci/BAO/WidgetCache.php b/CRM/Wci/BAO/WidgetCache.php index f33f5d1..513bc5e 100644 --- a/CRM/Wci/BAO/WidgetCache.php +++ b/CRM/Wci/BAO/WidgetCache.php @@ -113,37 +113,39 @@ class CRM_Wci_BAO_WidgetCache extends CRM_Wci_DAO_WidgetCache { } public static function setWidgetCache($widgetId, $code) { - $query = "DELETE from civicrm_wci_widget_cache WHERE widget_id = %1"; - CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), - TRUE, 'CRM_Wci_DAO_WidgetCache'); - - $query = "INSERT into civicrm_wci_widget_cache (widget_id, widget_code) VALUES (%1, %2)"; - $params = array(1=>array($widgetId, 'Integer'), - 2=>array($code, 'String'),); + $cacheTime = civicrm_api3('setting', 'getValue', + array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); + $expire_on = time() + ($cacheTime * 60); + $query = "INSERT INTO civicrm_wci_widget_cache (widget_id, widget_code, expire) + VALUES (%1, %2, %3) + ON DUPLICATE KEY UPDATE widget_id = %1, widget_code = %2, expire = %3"; + $params = array( + 1 => array($widgetId, 'Integer'), + 2 => array($code, 'String'), + 3 => array($expire_on, 'Integer') + ); CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_WidgetCache'); } - public static function getCurrentTsDiff($widgetId) { - $tsDiff = 0; - $query = "SELECT TIMESTAMPDIFF(MINUTE, `ts`, NOW()) as tsDiff FROM - civicrm_wci_widget_cache where widget_id = %1"; - $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), - TRUE, 'CRM_Wci_DAO_WidgetCache'); - if ($dao->fetch()) { - $tsDiff = $dao->tsDiff; - } else { - $tsDiff = PHP_INT_MAX; - } - return $tsDiff; - } public static function getWidgetCache($widgetId) { $code = ""; - $query = "SELECT widget_code FROM civicrm_wci_widget_cache where widget_id = %1"; - $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), - TRUE, 'CRM_Wci_DAO_WidgetCache'); + $query = "SELECT widget_code FROM civicrm_wci_widget_cache where widget_id = %1 + AND expire >= %2"; + $cacheTime = civicrm_api3('setting', 'getValue', + array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); + $expire_on = time() + ($cacheTime * 60); + $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($widgetId, 'Integer'), + 2 => array($expire_on, 'Integer')), TRUE, 'CRM_Wci_DAO_WidgetCache'); if ($dao->fetch()) { $code = $dao->widget_code; } return $code; } + + public static function deleteWidgetCache($widgetId) { + $code = ""; + $query = "DELETE FROM civicrm_wci_widget_cache where widget_id = %1"; + $dao = CRM_Core_DAO::executeQuery($query, + array(1 => array($widgetId, 'Integer')), TRUE, 'CRM_Wci_DAO_WidgetCache'); + } } diff --git a/CRM/Wci/DAO/WidgetCache.php b/CRM/Wci/DAO/WidgetCache.php index 379385e..7e8b2f6 100644 --- a/CRM/Wci/DAO/WidgetCache.php +++ b/CRM/Wci/DAO/WidgetCache.php @@ -134,16 +134,10 @@ class CRM_Wci_DAO_WidgetCache extends CRM_Core_DAO { if (!(self::$_fields)) { self::$_fields = array( - 'widget_cache_id' => array( - 'name' => 'id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('cahce Id', array('domain' => 'org.civicrm.wci')) , - 'required' => true, - ) , 'widget_id' => array( 'name' => 'widget_id', 'type' => CRM_Utils_Type::T_INT, - 'required' => false, + 'required' => true, ) , 'widget_code' => array( 'name' => 'widget_code', @@ -151,12 +145,11 @@ class CRM_Wci_DAO_WidgetCache extends CRM_Core_DAO 'title' => ts('widget code', array('domain' => 'org.civicrm.wci')) , 'required' => false, ) , - 'ts' => array( + 'expire' => array( 'name' => 'ts', - 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'type' => CRM_Utils_Type::T_INT, 'title' => ts('timestamp', array('domain' => 'org.civicrm.wci')) , 'required' => false, - 'maxlength' => 64, ) , @@ -175,10 +168,9 @@ class CRM_Wci_DAO_WidgetCache extends CRM_Core_DAO { if (!(self::$_fieldKeys)) { self::$_fieldKeys = array( - 'id' => 'widget_cache_id', 'widget_id' => 'widget_id', 'widget_code' => 'widget_code', - 'ts' => 'ts', + 'expire' => 'expire', ); } return self::$_fieldKeys; diff --git a/CRM/Wci/Form/CreateWidget.php b/CRM/Wci/Form/CreateWidget.php index 3acfed0..e0b3579 100644 --- a/CRM/Wci/Form/CreateWidget.php +++ b/CRM/Wci/Form/CreateWidget.php @@ -345,10 +345,16 @@ where w.id=" . $this->_id;*/ CRM_Core_DAO::executeQuery($sql, $params); CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;"); $transaction->commit(); + if (isset($this->_id)) { + $widget_id = $this->_id; + CRM_Wci_BAO_WidgetCache::deleteWidgetCache($widget_id); + } + else { + $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); + } CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success'); + if(isset($_REQUEST['_qf_CreateWidget_next'])) { - (isset($this->_id)) ? $widget_id = $this->_id : - $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()'); CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id); } else { CRM_Utils_System::redirect('widget?reset=1'); diff --git a/CRM/Wci/Form/WCISettings.php b/CRM/Wci/Form/WCISettings.php index 7e5001c..0a028ba 100644 --- a/CRM/Wci/Form/WCISettings.php +++ b/CRM/Wci/Form/WCISettings.php @@ -9,24 +9,6 @@ require_once 'CRM/Core/Form.php'; */ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { function buildQuickForm() { - - // add form elements - /*$this->add( - 'select', // field type - 'default_profile', // field name - 'Default profile', // field label - $this->getProfiles(), // list of options - true // is required - );*/ - - /*$this->add( - 'select', // field type - 'default_widget', // field name - 'Default widget', // field label - $this->getWidgets(), // list of options - false // is required - );*/ - $this->add('text', 'default_profile', ts('Default profile'),true)->setSize(45); $this->add('text', 'widget_cache_timeout', ts('Widget cache timeout'),true)->setSize(45); $this->addButtons(array( @@ -36,10 +18,9 @@ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { 'isDefault' => TRUE, ), )); - +$cacheTime = 1; $cacheTime = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); - $defProf = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_profile')); - + $defProf = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_profile')); /*$this->setDefaults(array( 'default_widget' => $widgetId));*/ $this->setDefaults(array( @@ -58,8 +39,8 @@ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { function postProcess() { $values = $this->exportValues(); /*civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_widget' => $values['default_widget'],));*/ - civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_profile' => $values['default_profile'],)); - civicrm_api3('setting', 'create', array('domain_id' => 1, 'widget_cache_timeout' => $values['widget_cache_timeout'],)); + civicrm_api3('setting', 'create', array('sequential' => 1, 'default_wci_profile' => $values['default_profile'])); + civicrm_api3('setting', 'create', array('sequential' => 1, 'widget_cache_timeout' => $values['widget_cache_timeout'])); CRM_Core_Session::setStatus(ts('Widget settings are saved to database'), '', 'success'); parent::postProcess(); } diff --git a/CRM/Wci/Upgrader.php b/CRM/Wci/Upgrader.php index d80fdcc..5663574 100644 --- a/CRM/Wci/Upgrader.php +++ b/CRM/Wci/Upgrader.php @@ -63,77 +63,14 @@ class CRM_Wci_Upgrader extends CRM_Wci_Upgrader_Base { public function upgrade_1001() { $this->ctx->log->info('Applying update 1001'); CRM_Core_DAO::executeQuery(' - CREATE TABLE IF NOT EXISTS civicrm_wci_widget_cache ( - id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "cache Id.", - widget_id int(10) unsigned DEFAULT NULL COMMENT "widget id.", - widget_code text DEFAULT NULL COMMENT "Widget code.", - ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) + CREATE TABLE IF NOT EXISTS `civicrm_wci_widget_cache` ( + `widget_id` int(10) unsigned NOT NULL COMMENT "widget id.", + `widget_code` text DEFAULT NULL COMMENT "Widget code.", + `expire` int(10) DEFAULT 0 COMMENT "A Unix timestamp indicating when the cache entry should expire.", + PRIMARY KEY (`widget_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; '); return TRUE; } - /** - * Example: Run an external SQL script - * - * @return TRUE on success - * @throws Exception - public function upgrade_4201() { - $this->ctx->log->info('Applying update 4201'); - // this path is relative to the extension base dir - $this->executeSqlFile('sql/upgrade_4201.sql'); - return TRUE; - } // */ - - - /** - * Example: Run a slow upgrade process by breaking it up into smaller chunk - * - * @return TRUE on success - * @throws Exception - public function upgrade_4202() { - $this->ctx->log->info('Planning update 4202'); // PEAR Log interface - - $this->addTask(ts('Process first step'), 'processPart1', $arg1, $arg2); - $this->addTask(ts('Process second step'), 'processPart2', $arg3, $arg4); - $this->addTask(ts('Process second step'), 'processPart3', $arg5); - return TRUE; - } - public function processPart1($arg1, $arg2) { sleep(10); return TRUE; } - public function processPart2($arg3, $arg4) { sleep(10); return TRUE; } - public function processPart3($arg5) { sleep(10); return TRUE; } - // */ - - - /** - * Example: Run an upgrade with a query that touches many (potentially - * millions) of records by breaking it up into smaller chunks. - * - * @return TRUE on success - * @throws Exception - public function upgrade_4203() { - $this->ctx->log->info('Planning update 4203'); // PEAR Log interface - - $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution'); - $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution'); - for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) { - $endId = $startId + self::BATCH_SIZE - 1; - $title = ts('Upgrade Batch (%1 => %2)', array( - 1 => $startId, - 2 => $endId, - )); - $sql = ' - UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker) - WHERE id BETWEEN %1 and %2 - '; - $params = array( - 1 => array($startId, 'Integer'), - 2 => array($endId, 'Integer'), - ); - $this->addTask($title, 'executeSql', $sql, $params); - } - return TRUE; - } // */ - } diff --git a/CRM/Wci/WidgetCode.php b/CRM/Wci/WidgetCode.php index d0751ce..e53c460 100644 --- a/CRM/Wci/WidgetCode.php +++ b/CRM/Wci/WidgetCode.php @@ -3,7 +3,7 @@ require_once '../wci-helper-functions.php'; class CRM_Wci_WidgetCode { - static function get_widget_realtime_code($widgetId, $preview) { + static function generate_widget_code($widgetId, $preview = 0) { $data = CRM_Wci_BAO_Widget::getWidgetData($widgetId); $template = CRM_Core_Smarty::singleton(); $template->assign('wciform', $data); @@ -17,27 +17,24 @@ class CRM_Wci_WidgetCode { $wcidata = $template->fetch('string:' . html_entity_decode($data['custom_template'])); } $code = json_encode($wcidata); - CRM_Wci_BAO_WidgetCache::setWidgetCache($widgetId, $code); return $code; } - static function get_widget_code($embedId, $preview=0) { - - if($preview) { - /**On preview time controller is called from create widget - form so id will be widget id */ - $code = CRM_Wci_WidgetCode::get_widget_realtime_code($embedId, $preview); - } else { + static function get_widget_code($embedId, $preview = 0) { + $code = ''; + if ($preview) { + return CRM_Wci_WidgetCode::generate_widget_code($embedId, $preview); + } + else { $widgetId = CRM_Wci_BAO_EmbedCode::getWidgetId($embedId); $code = CRM_Wci_BAO_WidgetCache::getWidgetCache($widgetId); - - $tsDiff = CRM_Wci_BAO_WidgetCache::getCurrentTsDiff($widgetId); - $cacheTime = civicrm_api3('setting', 'getValue', - array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); - if(($tsDiff > $cacheTime)||(empty($code))) { - $code = CRM_Wci_WidgetCode::get_widget_realtime_code($widgetId, $preview); - } } + + if (!$code) { + $code = CRM_Wci_WidgetCode::generate_widget_code($widgetId); + CRM_Wci_BAO_WidgetCache::setWidgetCache($widgetId, $code); + } + return $code; } } diff --git a/extern/embed.php b/extern/embed.php index 6f213fc..0ba73fb 100644 --- a/extern/embed.php +++ b/extern/embed.php @@ -32,15 +32,10 @@ * $Id$ */ - -/* - -*/ - require_once '../../../civicrm.config.php'; -$wciembed_js = ' -/* +$license_text = ' + /* @licstart Copyright (C) 2014 Zyxware Technologies @@ -55,7 +50,8 @@ $wciembed_js = ' @licend */ - + '; +$wciembed_js = ' // Cleanup functions for the document ready method if ( document.addEventListener ) { DOMContentLoaded = function() { @@ -100,9 +96,11 @@ $config = CRM_Core_Config::singleton(); $embedId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); $preview = CRM_Utils_Request::retrieve('preview', 'Positive', CRM_Core_DAO::$_nullObject); -$output = 'var wciwidgetcode = ' . CRM_Wci_WidgetCode::get_widget_code($embedId, $preview) . ';'; -$output = $output . $wciembed_js; +$output = $license_text; +$output .= 'var wciwidgetcode = ' . CRM_Wci_WidgetCode::get_widget_code($embedId, $preview) . ';'; +$output .= $wciembed_js; + echo $output; CRM_Utils_System::civiExit(); diff --git a/info.xml b/info.xml index 7086a8b..940f19f 100644 --- a/info.xml +++ b/info.xml @@ -12,9 +12,9 @@ 1.0-beta3 beta - 4.2 + 4.4 - + Contributed by Zyxware Technologies - www.zyxware.com CRM/Wci diff --git a/settings/wci.setting.php b/settings/wci.setting.php index 12158ab..276e8ae 100644 --- a/settings/wci.setting.php +++ b/settings/wci.setting.php @@ -1,26 +1,11 @@ array( - 'group_name' => 'Wci Preference', - 'group' => 'wci', - 'name' => 'default_wci_widget', - 'type' => 'Integer', - 'default' => 0, - 'add' => '4.3', - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => 'Default widget id', - 'help_text' => 'Sets default widget id', - ), 'default_wci_profile' => array( 'group_name' => 'Wci Preference', 'group' => 'wci', 'name' => 'default_wci_profile', 'type' => 'Integer', 'default' => 0, - 'add' => '4.3', - 'is_domain' => 1, - 'is_contact' => 0, 'description' => 'Default profile id', 'help_text' => 'Sets default profile id', ), @@ -30,10 +15,7 @@ return array( 'name' => 'widget_cache_timeout', 'type' => 'Integer', 'default' => 0, - 'add' => '4.3', - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => 'widget timeout', + 'description' => 'Widget timeout', 'help_text' => 'widget timeout', ), ); diff --git a/sql/install.sql b/sql/install.sql index a506792..d20d672 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -67,10 +67,9 @@ CREATE TABLE IF NOT EXISTS civicrm_wci_embed_code ( -- WCI widget cache. CREATE TABLE IF NOT EXISTS civicrm_wci_widget_cache ( - id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'cache Id.', - widget_id int(10) unsigned DEFAULT NULL COMMENT 'widget id.', + widget_id int(10) id int(10) unsigned NOT NULL COMMENT 'widget id.', widget_code text DEFAULT NULL COMMENT 'Widget code.', - ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) + expire int(10) DEFAULT 0 COMMENT 'A Unix timestamp indicating when the cache entry should expire.';, + PRIMARY KEY (`widget_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; diff --git a/templates/CRM/Wci/Form/CreateWidget.tpl b/templates/CRM/Wci/Form/CreateWidget.tpl index a920528..137e1c3 100644 --- a/templates/CRM/Wci/Form/CreateWidget.tpl +++ b/templates/CRM/Wci/Form/CreateWidget.tpl @@ -7,16 +7,6 @@ {if $form.title.value != ""} {php} if(isset($_REQUEST['id'])) { - /*$wid_id = $_REQUEST['id']; - $data = CRM_Wci_BAO_Widget::getWidgetData($wid_id); - $template = CRM_Core_Smarty::singleton(); - $template->assign('wciform', $data); - if($data["override"] == 0) { - $template->template_dir[] = getWciWidgetTemplatePath(); - $wcidata = $template->fetch('wciwidget.tpl'); - } else { - $wcidata = $template->fetch('string:' . $wid_page[$dao->id]['custom_template']); - }*/ $widget_controller_path = getWciWidgetControllerPath(); $extension_root_path = getExtensionRootPath(); } @@ -30,21 +20,9 @@
Click Save & Preview to save any changes to your settings, and preview the widget again on this page.
- - +
-
diff --git a/templates/CRM/Wci/Form/NewEmbedCode.tpl b/templates/CRM/Wci/Form/NewEmbedCode.tpl index 3e6a148..1269a31 100644 --- a/templates/CRM/Wci/Form/NewEmbedCode.tpl +++ b/templates/CRM/Wci/Form/NewEmbedCode.tpl @@ -18,15 +18,6 @@ if(isset($_REQUEST['id'])) { $emb_id = $_REQUEST['id']; $wid_id = CRM_Wci_BAO_EmbedCode::getWidgetId($emb_id); - $data = CRM_Wci_BAO_Widget::getWidgetData($wid_id); - $template = CRM_Core_Smarty::singleton(); - $template->assign('wciform', $data); - if($data["override"] == 0) { - $template->template_dir[] = getWciWidgetTemplatePath(); - $wcidata = $template->fetch('wciwidget.tpl'); - } else { - $wcidata = $template->fetch('string:' . $wid_page[$dao->id]['custom_template']); - } $widget_controller_path = getWciWidgetControllerPath(); $extension_root_path = getExtensionRootPath(); } @@ -41,7 +32,7 @@
Click Save & Preview to save any changes to your settings, and preview the widget again on this page.
- +
-- 2.25.1