|
From: <rgr...@us...> - 2014-01-25 02:53:48
|
Revision: 12276
http://sourceforge.net/p/xoops/svn/12276
Author: rgriffith
Date: 2014-01-25 02:53:45 +0000 (Sat, 25 Jan 2014)
Log Message:
-----------
More for #1268 - refine query to select most recently logged in users, adjust caching
Modified Paths:
--------------
XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselectuser.php
Modified: XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselectuser.php
===================================================================
--- XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselectuser.php 2014-01-23 21:02:28 UTC (rev 12275)
+++ XoopsCore/branches/2.5.x/2.5.7/htdocs/class/xoopsform/formselectuser.php 2014-01-25 02:53:45 UTC (rev 12276)
@@ -32,59 +32,80 @@
/**
* Constructor
*
- * @param string $caption
- * @param string $name
- * @param mixed $value Pre-selected value (or array of them).
- * For an item with massive members, such as "Registered Users", "$value" should be used to store selected temporary users only instead of all members of that item
- * @param bool $include_anon Include user "anonymous"?
- * @param int $size Number or rows. "1" makes a drop-down-list.
- * @param bool $multiple Allow multiple selections?
+ * @param string $caption form element caption
+ * @param string $name form element name
+ * @param bool $include_anon Include user "anonymous"?
+ * @param mixed $value Pre-selected value (or array of them).
+ * For an item with massive members, such as "Registered Users", "$value"
+ * should be used to store selected temporary users only instead of all
+ * members of that item
+ * @param int $size Number or rows. "1" makes a drop-down-list.
+ * @param bool $multiple Allow multiple selections?
*/
- function XoopsFormSelectUser($caption, $name, $include_anon = false, $value = null, $size = 1, $multiple = false)
+ public function XoopsFormSelectUser($caption, $name, $include_anon = false, $value = null, $size = 1, $multiple = false)
{
/**
- * @var array|null - cache potentially expensive result for this session.
- * Some modules use multiple copies of this element on a single page, and for
- * sites with large user bases, that can be quite expensive when $value is null.
- * @todo this should be replaced with a filter or autocomplete interface .
+ * @var mixed array|false - cache any result for this session.
+ * Some modules use multiple copies of this element on a single page, so this call will
+ * be made multiple times. This is only used when $value is null.
+ * @todo this should be replaced with better interface, with autocomplete style search
+ * and user specific MRU cache
*/
- static $querycache = null;
+ static $querycache = false;
+
/**
- * @var int|null - cache record count from member_handler
+ * @var int - limit to this many rows
*/
- static $countcache = null;
+ $limit = 200;
- $limit = 100;
+ /**
+ * @var string - cache time to live - will be interpreted by strtotime()
+ */
+ $cachettl = '+5 minutes';
+
+ /**
+ * @var string - cache key
+ */
+ $cachekey = 'formselectuser';
+
$select_element = new XoopsFormSelect('', $name, $value, $size, $multiple);
if ($include_anon) {
$select_element->addOption(0, $GLOBALS['xoopsConfig']['anonymous']);
}
$member_handler =& xoops_gethandler('member');
- if ($countcache===null) {
- $countcache = $member_handler->getUserCount();
- }
- $user_count = $countcache;
$value = is_array($value) ? $value : (empty($value) ? array() : array($value));
- if ($user_count > $limit && count($value) > 0) {
- $criteria = new CriteriaCompo(new Criteria('uid', '(' . implode(',', $value) . ')', 'IN'));
- $use_cache = false;
+ if (count($value) > 0) {
+ // simple case - we have a set of uids in $values
+ $criteria = new Criteria('uid', '(' . implode(',', $value) . ')', 'IN');
+ $criteria->setSort('uname');
+ $criteria->setOrder('ASC');
+ $users = $member_handler->getUserList($criteria);
} else {
- $use_cache = true;
- $criteria = new CriteriaCompo();
- $criteria->setLimit($limit);
- }
- $criteria->setSort('uname');
- $criteria->setOrder('ASC');
- if ($use_cache && !empty($querycache)) {
+ // open ended case - no selection criteria
+ // we will always cache this version to reduce expense
+ if (empty($querycache)) {
+ XoopsLoad::load('XoopsCache');
+ $querycache = XoopsCache::read($cachekey);
+ if ($querycache===false) {
+ $criteria = new CriteriaCompo();
+ if ($limit <= $member_handler->getUserCount()) {
+ // if we have more than $limit users, we will select who to show based on last_login
+ $criteria->setLimit($limit);
+ $criteria->setSort('last_login');
+ $criteria->setOrder('DESC');
+ } else {
+ $criteria->setSort('uname');
+ $criteria->setOrder('ASC');
+ }
+ $querycache = $member_handler->getUserList($criteria);
+ asort($querycache);
+ XoopsCache::write($cachekey, $querycache, $cachettl); // won't do anything different if write fails
+ }
+ }
$users = $querycache;
- } else {
- $users = $member_handler->getUserList($criteria);
- if ($use_cache) {
- $querycache = $users;
- }
}
$select_element->addOptionArray($users);
- if ($user_count <= $limit) {
+ if ($limit>count($users)) {
$this->XoopsFormElementTray($caption, "", $name);
$this->addElement($select_element);
|