diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index df60c75ac769a8f134809ee0952c9e0a1ce1a3f3..3c7325e9870c993807f5457d13eccbf887b5af51 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -46,8 +46,9 @@ Drupal x.x.x, xxxx-xx-xx (development version)
 - performance:
     * improved session handling: reduces database overhead.
     * improved access checking: reduces database overhead.
+    * made it possible to do memcached based session management.
     * omit sidebars when serving a '404 - Page not found': saves CPU cycles and bandwidth.
-    * added an 'aggressive' caching policy. 
+    * added an 'aggressive' caching policy.
 - removed the archive module.
 - upgrade system:
     * created space for update branches.
diff --git a/includes/module.inc b/includes/module.inc
index f25331730df4070a6e863ab475a9943a37567ff7..fecb9c68a6f7abdbf5b09dc97c06f4f67c43866a 100644
--- a/includes/module.inc
+++ b/includes/module.inc
@@ -128,6 +128,30 @@ function module_rebuild_cache() {
   return $files;
 }
 
+/**
+ * Parse Drupal meta file format.
+ * Uses ini parser provided by php's parse_ini_file().
+ * You should not use the meta files to store module specific settings:
+ * user <code>variable_get()</code> and <code>variable_set()</code> for those.
+ *
+ * Files should use the ini format to specify values:
+ *   key = "value"
+ *   key2 = value2
+ *
+ * @param $filename
+ *   The file we are parsing.  Accepts file with relative or absolute path.
+ * @return
+ *   The meta data array.
+ */
+function module_parse_meta_file($filename) {
+  $metadata = array();
+
+  if (file_exists($filename)) {
+    $metadata = parse_ini_file($filename);
+  }
+  return $metadata;
+}
+
 /**
  * Determine whether a given module exists.
  *
diff --git a/includes/session.inc b/includes/session.inc
index 4e219337e215f93aab6240411957246396715c10..17815eeb91b9e929a66c5c6b365e20d979417207 100644
--- a/includes/session.inc
+++ b/includes/session.inc
@@ -79,8 +79,43 @@ function sess_write($key, $value) {
   return TRUE;
 }
 
-function sess_destroy($key) {
-  db_query("DELETE FROM {sessions} WHERE sid = '%s'", $key);
+/**
+ * Called when an anonymous user becomes authenticated or vice-versa.
+ */
+function sess_regenerate() {
+  $old_session_id = session_id();
+  session_regenerate_id();
+  db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
+}
+
+/**
+ * Counts how many users have sessions. Can count either anonymous sessions, authenticated sessions, or both.
+ *
+ * @param int $timestamp
+ *   A Unix timestamp representing a point of time in the past.
+ *   The default is 0, which counts all existing sessions.
+ * @param int $anonymous
+ *   TRUE counts only anonymous users.
+ *   FALSE counts only authenticated users.
+ *   Any other value will return the count of both authenticated and anonymous users.
+ * @return  int
+ *   The number of users with sessions.
+ */
+function sess_count($timestamp = 0, $anonymous = true) {
+  $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0';
+  return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query, $timestamp));
+}
+
+/**
+ * Called by PHP session handling with the PHP session ID to end a user's session.
+ * Can also be called directly, either with the PHP session ID or another identifier
+ * such as uid to end a specific user's session.
+ *
+ * @param  string $uid
+ *   the user id
+ */
+function sess_destroy($uid) {
+  db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
 }
 
 function sess_gc($lifetime) {
diff --git a/modules/throttle/throttle.module b/modules/throttle/throttle.module
index 4707aa5806ca9853b2af4e480ff6e178b412a0b5..de21c1497ca753bba1cab38c0c37efc5d4a26733 100644
--- a/modules/throttle/throttle.module
+++ b/modules/throttle/throttle.module
@@ -63,13 +63,13 @@ function throttle_exit() {
     $throttle = module_invoke('throttle', 'status');
 
     if ($max_guests = variable_get('throttle_anonymous', 0)) {
-      $guests = db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
+      $guests = sess_count(time() - $time_period, TRUE);
     }
     else {
       $guests = 0;
     }
     if ($max_users = variable_get('throttle_user', 0)) {
-      $users = db_result(db_query('SELECT COUNT(DISTINCT(uid)) AS count FROM {sessions} WHERE timestamp >= %d AND uid != 0', time() - $time_period));
+      $users = sess_count(time() - $time_period, FALSE);
     }
     else {
       $users = 0;
diff --git a/modules/user/user.module b/modules/user/user.module
index 32eb5c0ba62d57495ccfe941342beed2a5628942..a61ba6b1aae11a5717efdf1ab542509c78d59571 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -148,7 +148,7 @@ function user_save($account, $array = array(), $category = 'account') {
 
     // Delete a blocked user's sessions to kick them if they are online.
     if (isset($array['status']) && $array['status'] == 0) {
-      db_query('DELETE FROM {sessions} WHERE uid = %d', $account->uid);
+      sess_destroy($account->uid);
     }
 
     // Refresh user object
@@ -560,24 +560,25 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
       case 3:
         if (user_access('access content')) {
           // Count users with activity in the past defined period.
-          $time_period = variable_get('user_block_seconds_online', 900);
+          $time_period = time() - variable_get('user_block_seconds_online', 900);
 
           // Perform database queries to gather online user lists.
-          $guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
-          $users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
-          $total_users = db_num_rows($users);
+          $anonymous_count = sess_count($time_period);
+          $authenticated_count = sess_count($time_period, false);
+          $authenticated_users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
+
 
           // Format the output with proper grammar.
-          if ($total_users == 1 && $guests->count == 1) {
-            $output = t('There is currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
+          if ($anonymous_count == 1 && $authenticated_count == 1) {
+            $output = t('There is currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
           }
           else {
-            $output = t('There are currently %members and %visitors online.', array('%members' => format_plural($total_users, '1 user', '@count users'), '%visitors' => format_plural($guests->count, '1 guest', '@count guests')));
+            $output = t('There are currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
           }
 
           // Display a list of currently online users.
           $max_users = variable_get('user_block_max_list_count', 10);
-          if ($total_users && $max_users) {
+          if ($authenticated_count && $max_users) {
             $items = array();
 
             while ($max_users-- && $account = db_fetch_object($users)) {
@@ -929,10 +930,7 @@ function user_login_submit($form_id, $form_values) {
 
     user_module_invoke('login', $form_values, $user);
 
-    $old_session_id = session_id();
-    session_regenerate_id();
-    db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id);
-
+    sess_regenerate();
   }
 }
 
@@ -993,7 +991,7 @@ function user_logout() {
   watchdog('user', t('Session closed for %name.', array('%name' => $user->name)));
 
   // Destroy the current session:
-  session_destroy();
+  sess_destroy($user->uid);
   module_invoke_all('user', 'logout', NULL, $user);
 
   // We have to use $GLOBALS to unset a global variable:
@@ -1433,8 +1431,8 @@ function user_confirm_delete($name, $uid) {
  */
 function user_delete($edit, $uid) {
   $account = user_load(array('uid' => $uid));
+  sess_destroy($uid);
   db_query('DELETE FROM {users} WHERE uid = %d', $uid);
-  db_query('DELETE FROM {sessions} WHERE uid = %d', $uid);
   db_query('DELETE FROM {users_roles} WHERE uid = %d', $uid);
   db_query('DELETE FROM {authmap} WHERE uid = %d', $uid);
   $array = array('%name' => $account->name, '%email' => '<'. $account->mail .'>');