Redis實戰(四)

來源:互聯網
上載者:User

標籤:

配置好了web.config程式,並且能通過C#代碼來讀取和管理以上配置資訊。

接下來,就可以進行Redis的資料寫入了。Redis中可以用Store和StoreAll分別儲存單條和多條資料,C#中具體代碼如下:
 protected void btnOpenDB_Click(object sender, EventArgs e)        {            //System.Diagnostics.Process.Start("D:\\redis\\redis-server.exe");            //lblShow.Text = "Redis已經開啟!";            using (var redisClient = RedisManager.GetClient())            {                var user = redisClient.GetTypedClient<User>();                if (user.GetAll().Count > 0)                    user.DeleteAll();                var qiujialong = new User                {                    Id = user.GetNextSequence(),                    Name = "qiujialong",                    Job = new Job { Position = ".NET" }                };                var chenxingxing = new User                {                    Id = user.GetNextSequence(),                    Name = "chenxingxing",                    Job = new Job { Position = ".NET" }                };                var luwei = new User                {                    Id = user.GetNextSequence(),                    Name = "luwei",                    Job = new Job { Position = ".NET" }                };                var zhourui = new User                {                    Id = user.GetNextSequence(),                    Name = "zhourui",                    Job = new Job { Position = "Java" }                };                var userToStore = new List<User> { qiujialong, chenxingxing, luwei, zhourui };                user.StoreAll(userToStore);                              lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";            }        }

2、儲存單條資料

protected void btnInsert_Click(object sender, EventArgs e)        {            if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtPosition.Text))            {                using (var redisClient = RedisManager.GetClient())                {                    var user = redisClient.GetTypedClient<User>();                    var newUser = new User                    {                        Id = user.GetNextSequence(),                        Name = txtName.Text,                        Job = new Job { Position = txtPosition.Text }                    };                                      user.Store(newUser);                                       if (user.GetAll().Count > 0)                    {                        var htmlStr = string.Empty;                        foreach (var u in user.GetAll())                        {                            htmlStr += "<li>ID=" + u.Id + "&nbsp;&nbsp;姓名:" + u.Name + "&nbsp;&nbsp;所在部門:" + u.Job.Position + "</li>";                        }                        lblPeople.Text = htmlStr;                    }                    lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";                }            }        }

 

 

Redis實戰(四)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.