php操作MongoDB类实例

本文实例讲述了php操作MongoDB类的方法。分享给大家供大家参考。具体如下:

1. MyMongo.php文件:

<?php
/**
 * 仿写CI的MongoDB
 * @author sparkHuang 2011-11-03
 *
 */
class MyMongo {
  private $mongo_config = "mongo_config.php";
  private $connection;
  private $db;
  private $mongo_connect_string;
  private $host;
  private $port;
  private $user;
  private $pass;
  private $dbname;
  private $persist;
  private $persist_key;
  private $selects = array();
  private $wheres = array();
  private $sorts = array();
  private $limit = 999999;
  private $offset = 0;
  public function __construct() {
    if ( ! class_exists('Mongo')) {
      $this->log_error("The MongoDB PECL extentiosn has not been installed or enabled.");
      exit;
    }

    $this->connection_string();
    $this->connect();
  }
  /**
   * 更改数据库
   *
   */
  public function switch_db($database = '') {
    if (empty($database)) {
      $this->log_error("To switch MongoDB databases, a new database name must be specified");
      exit;
    }
    $this->dbname = $database;
    try {
      $this->db = $this->connection->{$this->dbname};
      return true;
    } catch(Exception $e) {
      $this->log_error("Unable to switch Mongo Databases: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * 设置select字段
   *
   */
  public function select($includs = array(), $excludes = array()) {
    if ( ! is_array($includs)) {
      $includs = (array)$includs;
    }

    if ( ! is_array($excludes)) {
      $excludes = (array)$excludes;
    }

    if ( ! empty($includs)) {
      foreach ($includs as $col) {
        $this->selects[$col] = 1;
      }
    } else {
      foreach ($excludes as $col) {
        $this->selects[$col] = 0;
      }
    }

    return($this);
  }
  /**
   * where条件查询判断
   *
   * @usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');
   *
   */
  public function where($wheres = array()) {
    if ( ! is_array($wheres)) {
      $wheres = (array)$wheres;
    }

    if ( ! empty($wheres)) {
      foreach($wheres as $wh => $val) {
        $this->wheres[$wh] = $val;
      }
    }

    return($this);
  }
  /**
   * where ... in .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_in('foo', array('bar', 'zoo'))->get('foobar');
   *
   */
  public function where_in($field = '', $in = array()) {
    $this->where_init($field);
    $this->wheres[$field]['$in'] = $in;
    return($this);
  }
  /**
   * where ... not in .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_not_in('foo', array('bar', 'zoo'))->get('foobar');
   *
   */
  public function where_not_in($field = '', $in = array()) {
    $this->where_init($field);
    $this->wheres[$field]['$nin'] = $in;
    return($this);
  }
  /**
   * where ... $field > $x .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_gt('foo', 20)->get('foobar');
   *
   */
  public function where_gt($field = '', $x) {
    $this->where_init($field);
    $this->wheres[$field]['$gt'] = $x;
    return($this);
  }
  /**
   * where ... $field >= $x .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_gte('foo', 20)->get('foobar');
   *
   */
  public function where_gte($field = '', $x) {
    $this->where_init($field);
    $this->wheres[$field]['$gte'] = $x;
    return($this);
  }
  /**
   * where ... $field < $x .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_lt('foo', 20)->get('foobar');
   *
   */
  public function where_lt($field = '', $x) {
    $this->where_init($field);
    $this->wheres[$field]['$lt'] = $x;
    return($this);
  }
  /**
   * where ... $field <= $x .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_lte('foo', 20)->get('foobar');
   *
   */
  public function where_lte($field = '', $x) {
    $this->where_init($field);
    $this->wheres[$field]['$lte'] = $x;
    return($this);
  }
  /**
   * where ... $field >= $x AND $field <= $y .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_between('foo', 20, 30)->get('foobar');
   *
   */
  public function where_between($field = '', $x, $y) {
    $this->where_init($field);
    $this->wheres[$field]['$gte'] = $x;
    $this->wheres[$field]['$lte'] = $y;
    return($this);
  }
  /**
   * where ... $field > $x AND $field < $y .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_between_ne('foo', 20, 30)->get('foobar');
   *
   */
  public function where_between_ne($field = '', $x, $y) {
    $this->where_init($field);
    $this->wheres[$field]['$gt'] = $x;
    $this->wheres[$field]['$lt'] = $y;
    return($this);
  }
  /**
   * where ... $field <> $x .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_ne('foo', 20)->get('foobar');
   *
   */
  public function where_ne($field = '', $x) {
    $this->where_init($field);
    $this->wheres[$field]['$ne'] = $x;
    return($this);
  }
  /**
   * where ... or .. 条件查询判断
   *
   * @usage = $this->mongo_db->where_or('foo', array('foo', 'bar'))->get('foobar');
   *
   */
  public function where_or($field = '', $values) {
    $this->where_init($field);
    $this->wheres[$field]['$or'] = $values;
    return($this);
  }
  /**
   *  where ... and .. 条件查询判断
   *
   *  @usage = $this->mongo_db->where_and( array ( 'foo' => 1, 'b' => 'someexample' );
   */
   public function where_and( $elements_values = array() ) {
     foreach ( $elements_values as $element => $val ) {
       $this->wheres[$element] = $val;
     }
     return($this);
   }
  /**
   *  where $field % $num = $result
   *
   *  @usage = $this->mongo_db->where_mod( 'foo', 10, 1 );
   */
   public function where_mod( $field, $num, $result ) {
     $this->where_init($field);
     $this->wheres[$field]['$mod'] = array($num, $result);
     return($this);
   }
  /**
   * where size
   *
   *  Get the documents where the size of a field is in a given $size int
   *
   *  @usage : $this->mongo_db->where_size('foo', 1)->get('foobar');
   */
  public function where_size($field = "", $size = "") {
    $this->where_init($field);
    $this->wheres[$field]['$size'] = $size;
    return ($this);
  }
  /**
   * like条件查询(PHP中定义MongoRegex类实现)
   *
   * @usage : $this->mongo_db->like('foo', 'bar', 'im', false, false)->get();
   */
  public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = true, $enable_end_wildcard = true) {
    $field = (string)$field;
    $this->where_init($field);
    $value = (string)$value;
    $value = quotmeta($value);

    if (true !== $enable_start_wildcard) {
      $value = "^".$value;
    }

    if (true !== $enable_end_wildcard) {
      $value .= "$";
    }

    $regex = "/$value/$flags";
    $this->wheres[$field] = new MongoRegex($regex);
    return($this);
  }
  /**
   * order排序( 1 => ASC, -1 => DESC)
   *
   * @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->order_by(array("age" => 1));
   */
  public function order_by($fields = array()) {
    foreach($fields as $col => $val) {
      if ($val == -1 || $val == false || strtolower($val) == "desc") {
        $this->sorts[$col] = -1;
      } else {
        $this->sorts[$col] = 1;
      }
    }
    return($this);
  }
  /**
   * limit
   *
   * @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->limit(10);
   */
  public function limit($x = 999999) {
    if ($x !== NULL && is_numeric($x) && $x >= 1) {
      $this->limit = (int)$x;
    }
    return($this);
  }
  /**
   * offset
   *
   * @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->offset(10);
   */
  public function offset($x = 0) {
     if($x !== NULL && is_numeric($x) && $x >= 1) {
       $this->offset = (int) $x;
     }
     return($this);
  }
  /**
   * get_where
   *
   * @usage: $this->mongo_db->get_where('foo', array('bar' => 'something'));
   */
  public function get_where($collection = "", $where = array(), $limit = 999999) {
    return($this->where($where)->limit($limit)->get($collection));
  }
  /**
   * get
   *
   * @usage: $this->mongo_db->where(array('name' => 'tom'))->get('foo');
   */
  public function get($collection) {
    if (empty($collection)) {
      $this->log_error("In order to retreive documents from MongoDB, a collection name must be passed");
      exit;
    }
    $results = array();
    $results = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int)$this->limit)->skip((int)$this->offset)->sort($this->sorts);
    $returns = array();
    foreach($results as $result) {
      $returns[] = $result;
    }
    $this->clear();
    return($returns);
  }
  /**
   * count
   *
   * @usage: $this->db->get_where('foo', array('name' => 'tom'))->count('foo');
   */
  public function count($collection) {
    if (empty($collection)) {
      $this->log_error("In order to retreive documents from MongoDB, a collection name must be passed");
      exit;
    }
    $count = $this->db->{$collection}->find($this->wheres)->limit((int)$this->limit)->skip((int)$this->offset)->count();
    $this->clear();
    return($count);
  }
  /**
   * insert
   *
   * @usage: $this->mongo_db->insert('foo', array('name' => 'tom'));
   */
  public function insert($collection = "", $data = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection selected to delete from");
      exit;
    }
    if (count($data) == 0 || ! is_array($data)) {
      $this->log_error("Nothing to insert into Mongo collection or insert is not an array");
      exit;
    }
    try {
      $this->db->{$collection}->insert($data, array('fsync' => true));
      if (isset($data['_id'])) {
        return($data['_id']);
      } else {
        return(false);
      }
    } catch(MongoCursorException $e) {
      $this->log_error("Insert of data into MongoDB failed: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * update : 利用MongoDB的 $set 实现
   *
   * @usage : $this->mongo_db->where(array('name' => 'tom'))->update('foo', array('age' => 24))
   */
  public function update($collection = "", $data = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection selected to delete from");
      exit;
    }
    if (count($data) == 0 || ! is_array($data)) {
      $this->log_error("Nothing to update in Mongo collection or update is not an array");
      exit;
    }
    try {
      $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => true, 'multiple' => false)); //注意: multiple为false
      return(true);
    } catch(MongoCursorException $e) {
      $this->log_error("Update of data into MongoDB failed: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * update_all : 利用MongoDB的 $set 实现
   *
   * @usage : $this->mongo_db->where(array('name' => 'tom'))->update_all('foo', array('age' => 24));
   */
  public function update_all($collection = "", $data = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection selected to delete from");
      exit;
    }
    if (count($data) == 0 || ! is_array($data)) {
      $this->log_error("Nothing to update in Mongo collection or update is not an array");
      exit;
    }
    try {
      $this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => true, 'multiple' => true)); //注意: multiple为true
      return(true);
    } catch(MongoCursorException $e) {
      $this->log_error("Update of data into MongoDB failed: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * delete
   *
   * @usage : $this->mongo_db->where(array('name' => 'tom'))->delete('foo');
   */
  public function delete($collection = "") {
    if (empty($collection)) {
      $this->log_error("No Mongo collection selected to delete from");
      exit;
    }
    try {
      $this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justOne' => true)); //注意justOne为true;
    } catch(MongoCursorException $e) {
      $this->log_error("Delete of data into MongoDB failed: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * delete_all
   *
   * @usage : $this->mongo_db->where(array('name' => 'tom'))->delete_all('foo');
   */
  public function delete_all($collection = "") {
    if (empty($collection)) {
      $this->log_error("No Mongo collection selected to delete from");
      exit;
    }
    try {
      $this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justOne' => false)); //注意justOne为false;
    } catch(MongoCursorException $e) {
      $this->log_error("Delete of data into MongoDB failed: {$e->getMessage()}");
      exit;
    }
  }
  /**
   * add_index
   *
   * @usage : $this->mongo_db->add_index('foo', array('first_name' => 'ASC', 'last_name' => -1), array('unique' => true)));
   */
  public function add_index($collection, $keys = array(), $options = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection specified to add index to");
      exit;
    }
    if (empty($keys) || ! is_array($keys)) {
      $this->log_error("Index could not be created to MongoDB Collection because no keys were specified");
      exit;
    }
    foreach($keys as $col => $val) {
      if ($val == -1 || $val == false || strtolower($val) == 'desc') {
        $keys[$col] = -1;
      } else {
        $keys[$col] = 1;
      }
    }
    //在此没有对$options数组的有效性进行验证
    if (true == $this->db->{$collection}->ensureIndex($keys, $options)) {
      $this->clear();
      return($this);
    } else {
      $this->log_error("An error occured when trying to add an index to MongoDB Collection");
      exit;
    }
  }
  /**
   * remove_index
   *
   * @usage : $this->mongo_db->remove_index('foo', array('first_name' => 'ASC', 'last_name' => -1))
   */
  public function remove_index($collection = "", $keys = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection specified to add index to");
      exit;
    }
    if (empty($keys) || ! is_array($keys)) {
      $this->log_error("Index could not be created to MongoDB Collection because no keys were specified");
      exit;
    }
    if ($this->db->{$collection}->deleteIndex($keys)) {
      $this->clear();
      return($this);
    } else {
      $this->log_error("An error occured when trying to add an index to MongoDB Collection");
      exit;
    }
  }
  /**
   * remove_all_index
   *
   * @usage : $this->mongo_db->remove_all_index('foo', array('first_name' => 'ASC', 'last_name' => -1))
   */
  public function remove_all_index($collection = "", $keys = array()) {
    if (empty($collection)) {
      $this->log_error("No Mongo collection specified to add index to");
      exit;
    }
    if (empty($keys) || ! is_array($keys)) {
      $this->log_error("Index could not be created to MongoDB Collection because no keys were specified");
      exit;
    }
    if ($this->db->{$collection}->deleteIndexes($keys)) {
      $this->clear();
      return($this);
    } else {
      $this->log_error("An error occured when trying to add an index to MongoDB Collection");
      exit;
    }
  }
  /**
   * list_indexes
   *
   * @usage : $this->mongo_db->list_indexes('foo');
   */
  public function list_indexes($collection = "") {
    if (empty($collection)) {
      $this->log_error("No Mongo collection specified to add index to");
      exit;
    }
    return($this->db->{$collection}->getIndexInfo());
  }
  /**
   * drop_collection
   *
   * @usage : $this->mongo_db->drop_collection('foo');
   */
  public function drop_collection($collection = "") {
    if (empty($collection)) {
      $this->log_error("No Mongo collection specified to add index to");
      exit;
    }
    $this->db->{$collection}->drop();
    return(true);
  }
  /**
   * 生成连接MongoDB 参数字符串
   *
   */
  private function connection_string() {
    include_once($this->mongo_config);
    $this->host = trim($config['host']);
    $this->port = trim($config['port']);
    $this->user = trim($config['user']);
    $this->pass = trim($config['pass']);
    $this->dbname = trim($config['dbname']);
    $this->persist = trim($config['persist']);
    $this->persist_key = trim($config['persist_key']);
    $connection_string = "mongodb://";
    if (empty($this->host)) {
      $this->log_error("The Host must be set to connect to MongoDB");
      exit;
    }
    if (empty($this->dbname)) {
      $this->log_error("The Database must be set to connect to MongoDB");
      exit;
    }
    if ( ! empty($this->user) && ! empty($this->pass)) {
      $connection_string .= "{$this->user}:{$this->pass}@";
    }
    if ( isset($this->port) && ! empty($this->port)) {
      $connection_string .= "{$this->host}:{$this->port}";
    } else {
      $connection_string .= "{$this->host}";
    }
    $this->connection_string = trim($connection_string);
  }
  /**
   * 连接MongoDB 获取数据库操作句柄
   *
   */
  private function connect() {
    $options = array();
    if (true === $this->persist) {
      $options['persist'] = isset($this->persist_key) && ! empty($this->persist_key) ? $this->persist_key : "ci_mongo_persist";
    }
    try {
      $this->connection = new Mongo($this->connection_string, $options);
      $this->db = $this->connection->{$this->dbname};
      return ($this);
    } catch (MongoConnectionException $e) {
      $this->log_error("Unable to connect to MongoDB: {$e->getMessage()}");
    }
  }
  /**
   * 初始化清理部分成员变量
   *
   */
  private function clear() {
    $this->selects = array();
    $this->wheres = array();
    $this->limit = NULL;
    $this->offset = NULL;
    $this->sorts = array();
  }
  /**
   * 依据字段名初始化处理$wheres数组
   *
   */
  private function where_init($param) {
    if ( ! isset($this->wheres[$param])) {
      $this->wheres[$param] = array();
    }
  }
  /**
   * 错误记录
   *
   */
  private function log_error($msg) {
    $msg = "[Date: ".date("Y-m-i H:i:s")."] ".$msg;
    @file_put_contents("./error.log", print_r($msg."\n", true), FILE_APPEND);
  }
}
/* End of MyMongo.php */

2. mongo_config.php配置文件:

<?php
$config["host"] = "localhost";
$config["user"] = "";
$config["pass"] = "";
$config["port"] = 27017;
$config["dbname"] = "test";
$config['persist'] = TRUE;
$config['persist_key'] = 'ci_mongo_persist';
/*End of mongo_config.php*/

3. MyMongoDemo.php文件:

<?php
include_once("MyMongo.php");
$conn = new MyMongo();
//删除所有记录
$conn->delete_all("blog");
//插入第一条记录
$value = array("name" => "小明", "age" => 25, "addr" => array("country" => "中国", "province" => "广西", "city" => "桂林"));
$conn->insert("blog", $value);
var_dump($conn->select(array("name", "age"))->get("blog"));
var_dump($conn->get("blog"));
/* End of MyMongoDemo.php */

希望本文所述对大家的php程序设计有所帮助。

(0)

相关推荐

  • php实现的mongodb操作类

    mongo_db.php <?php /** * Created by PhpStorm. * User: yangyulong * Date: 2015/5/26 * Time: 13:45 */ class Mongo_db { private static $instanceof = NULL; public $mongo; private $host = 'localhost'; private $port = '27017'; private $db; public $dbname;

  • 高效mongodb的php分页类(不使用skip)

    mongodb分页skip+limit分页要先查出所有结果再去跳过,这样如果查询页面越往后效率越低. 如果能够通过查询条件查出每页结果的最后一条记录,在用最后一条记录作为查询条件去查下一页,这样每次都查询页面size条记录,效率不会差. 具体代码如下:包含mongodb.class.php, page.class.php, test.php mongodb.class.php mongodb 操作类 复制代码 代码如下: <?php function show_error($message, $

  • php实现的mongodb操作类实例

    本文实例讲述了php实现的mongodb操作类.分享给大家供大家参考.具体如下: <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ class mongo_db { private $config; private $connection; private $db; private $connection_string; private $h

  • php mongodb操作类 带几个简单的例子

    之前我们已经发过几篇类似的文章,大家可以参考一下. 核心代码: class NewMongodb { private $mongo; //NewMongodb连接 private $curr_db_name; private $curr_table_name; private $error; public $config; public function getInstance($mongo_server, $flag=array()) { static $NewMongodb_arr; if

  • php封装的mongodb操作类代码

    核心代码 <?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ class mongo_db { private $config; private $connection; private $db; private $connection_string; private $host; private $port; private $user; p

  • PHP实现的MongoDB数据库操作类分享

    class HMongodb { private $mongo; //Mongodb连接 private $curr_db_name; private $curr_table_name; private $error; public function getInstance($mongo_server, $flag=array()) { static $mongodb_arr; if (empty($flag['tag'])) { $flag['tag'] = 'default'; } if (

  • php操作MongoDB类实例

    本文实例讲述了php操作MongoDB类的方法.分享给大家供大家参考.具体如下: 1. MyMongo.php文件: <?php /** * 仿写CI的MongoDB * @author sparkHuang 2011-11-03 * */ class MyMongo { private $mongo_config = "mongo_config.php"; private $connection; private $db; private $mongo_connect_stri

  • php文件操作相关类实例

    本文实例讲述了php文件操作相关类.分享给大家供大家参考.具体如下: <?php class file_dir { function check_exist($filename) //检查目录或文件是否存在 { if(file_exists($filename)) { return true; } else return false; } function create_dir($dirname,$mode=0777) // 一次只能创建一级目录 { if(is_null($dirname) |

  • JavaScript操作cookie类实例

    本文实例讲述了JavaScript操作cookie类.分享给大家供大家参考.具体如下: 用法: 一.设置cookie var cookie = new JSCookie(); // 普通设置 cookie .SetCookie("key1","val1"); // 过期时间为一年 var expire_time = new Date(); expire_time.setFullYear(expire_time.getFullYear() + 1); cookie .

  • C#自定义的字符串操作增强类实例

    本文实例讲述了C#自定义的字符串操作增强类.分享给大家供大家参考.具体如下: 这个C#类在C#自由的字符串操作类的基础上进行的大幅度增强,把我们平时可能用到的字符串操作都做进去了,字符串的处理我想大部分编程都不可避免,有了这个类,可以节省你很多时间,同时可以根据自己的需要对这个C#字符串类进行扩展. using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpress

  • Node.js操作MongoDB数据库实例分析

    本文实例讲述了Node.js操作MongoDB数据库.分享给大家供大家参考,具体如下: Node.js操作MongoDB npm init npm i mongodb --save { "name": "test", "version": "1.0.0", "description": "", "main": "app.js", "scr

  • node.js操作MongoDB的实例详解

    node.js操作MongoDB时,需要安装mongodb包 1.使用npm安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 2.使用cnpm安装mongodb包 cnpm install mongodb node.js操作MongoDB时的四种方式:插入数据.更新数据.删除数据.查找数据. 操作步骤 步骤1 创建执行文件xx.js 步骤2 终端调用执行文件 node xx.js 注意: 操作前需要启动服务

  • Lua 操作 MongoDB 数据库实例

    最近有个工作是使用Nginx + Lua实现一个操作MongoDB数据库的API,主要实现其count和query功能.之前没有写过Lua,于是也就勉强着上手,在cloudwu的 lua-mongo 的基础上实现了操作MongoDB的API. cloudwu的lua-mongo驱动实现了连接Mongo,进行find和findOne等基本操作的功能,所以在lua-mongo的基础上增加了count和query等方法.修改的具体内容如下: 1.API基于luajit-2.0开发,相当于lua 5.1

  • PHP数据库操作Helper类完整实例

    本文实例讲述了PHP数据库操作Helper类.分享给大家供大家参考,具体如下: php操作数据库分为几个步骤(这里以MYSQL为例): 1. 建立连接 $connection=mysql_connect($db_host,$db_username,$db_password); 2. 选择数据库 $db_select=mysql_select_db($db_database); 3. 执行CRUD操作 mysql_query("set names 'utf8'");//编码 $resul

  • PHP操作Mongodb封装类完整实例

    本文实例讲述了PHP操作Mongodb封装类.分享给大家供大家参考,具体如下: <?php /** * Mongodb 基本操作API,支持基本类似关系统型数据库的操作接口 * * @version 1.0 * [说明] * * 1:该版本API实现了 Mongodb 中最基本的插入/修改/查询/删除操作的封装 * 2:其它更高级的操作可通过 $this->getMongo() 得到原生的对象,更多API请自行查阅 Mongo PHP手册,后续版本将会对增加更多的原生API封装 * 3:该类所

  • Asp.Net中Cache操作类实例详解

    本文以一个Asp.Net的Cache操作类实例代码来详细描述了cache缓存的结构及实现方法,完整代码如下所示: /// <head> /// <function> /// 存储类(存储UserInfo信息) /// </function> /// <description> /// 用Cache存储用户信息 /// 在指定间隔(TimeOut)内取,则可以从Cache中取, /// 如果超出存储时间,则从数据库取用户信息数据 /// 作為所有用户信息的存儲

随机推荐