缓存到Redis中记录数据

一。先配好 Redis

http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2011/0406/7873.html

     我自己配好的地址。

 <appSettings>
    <add key="RedisUrl" value="10.10.4.50:7895" /> //域名
    <add key="Redisdata" value="12" /> //value=12 是第12个库
  </appSettings>

 写cs文件

  public static class RedisPool
    {
        public static PooledRedisClientManager poolreds;
        static int port = Convert.ToInt32(ConfigurationManager.AppSettings["Redisdata"].ToString());
        static RedisPool()
        {
            try
            {
                poolreds = new PooledRedisClientManager(port, new string[] { ConfigurationManager.AppSettings["RedisUrl"] });
            }
            catch (Exception ex)
            {
                //PublicFunction.Log("" + ex.Message + "\r\n");
                //PublicFunction.Log("" + ex.StackTrace + "\r\n");
            }
        }

        /// <summary>
        /// 缓存到Redis中
        /// </summary>
        /// <param name="key"></param>
        /// <param name="danhaoList"></param>
        public static void EnqueueList(string key, string obj)
        {
            using (var client = poolreds.GetClient())
            {
                //队列使用 
                if (obj != null)
                {
                     client.AddItemToList(key, obj);
                }

                //client.Add(key, obj);
            }
        }

        /// <summary>
        /// 读取当前
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DequeueModel(string key)
        {
            using (var client = poolreds.GetClient())
            {
                string danhao = client.GetItemFromList(key,0);
                return danhao;
            }
        }

        /// <summary>
        /// 判断当前是否有记录
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long GetListCount(string key)
        {
            using (var client = poolreds.GetClient())
            {
                long count = client.GetListCount(key);
                return count;
            }
        }

        /// <summary>
        /// 清除Redis数据
        /// </summary>
        /// <returns></returns>
        public static void PopRedis()
        {
            using (var client = poolreds.GetClient())
            {
                client.FlushDb();//清除Redis的数据
            }
        }

    }

 前台调用。

  RedisPool.EnqueueList( "lin"+DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString());
            label1.Text = "昨天点击次数:" + RedisPool.GetListCount("lin" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"));
            label2.Text = "今天点击次数:" + RedisPool.GetListCount("lin" + DateTime.Now.ToString("yyyy-MM-dd"));

 

posted @ 2015-04-09 22:02  爱上海绵宝宝的派大星  阅读(317)  评论(0编辑  收藏  举报