In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Enyim.Caching to access Memcached. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
(1) download EnyimMemcached (file name: EnyimMemcached-master.zip) first.
When you go to the URL https://github.com/enyim, you can see the following interface and select "EnyimMemcached"
Go to the following page, or visit directly: https://github.com/enyim/EnyimMemcached, click "Download ZIP" on the right to get the file "EnyimMemcached-master.zip"
(2) decompress "EnyimMemcached-master.zip", as shown below
Note: we can search with "* .dll" and find that there is no Enyim.Caching.dll, so we need to generate it ourselves.
Open the "Enyim.Caching.sln" file with Visual Studio 2010
In the course of opening it, I came across the following hint (several times). At my current level, I still don't know exactly what will happen to this hint, so I ignored it. If you have any friends who understand, you can tell me.
Directly, if we click F6 (generate solution), we will find the following error
In response to the above problems, we can look at the reference information for each project in solution Explorer on the right and find
-- > references to log4net in the Enyim.Caching.Log4NetAdaper project are missing
-- > references to nuit.framework and nuit.mocks in the MemcachedTest project are lost
For this problem, we can solve it by Nuget
Take another look, and find more errors, as shown in the following figure, which turns out to be "the namespace of NUit was not found"
Use Nuget to solve this NUnit problem, search for nunit.mocks, install it, and then press F6 (generate solution) to find that it is successful.
(3) generate the Release version of Enyim.Caching.dll and find the Enyim.Caching.dll file in the Bin\ Release directory of Enyim.Caching
Enyim.Caching\ bin\ Release
(4) create a new "console application" and place Enyim.Caching.dll in the project's bin directory
LearningEnyimMemcached\ bin
Add a reference to Enyim.Caching
(5) add an "application configuration file" with the file name App.config
The configuration in App.config is as follows
(6) Code in Program.cs file
As follows
Using System;using Enyim.Caching;using Enyim.Caching.Memcached;namespace LearningEnyimMemcached {class Program {static void Main (string [] args) {/ / create a MemcachedClient in your application / / you can cache the client in a static variable or just recreate it every time MemcachedClient mc = new MemcachedClient (); / / store a string in the cache mc.Store (StoreMode.Set, "MyKey", "Hello") / / retrieve the item from the cache Console.WriteLine (mc.Get ("MyKey")); / / store some other items mc.Store (StoreMode.Set, "D1", 1234L); mc.Store (StoreMode.Set, "D2", DateTime.Now); mc.Store (StoreMode.Set, "D3", true) Mc.Store (StoreMode.Set, "D4", new Product ()); mc.Store (StoreMode.Set, "D5", new byte [] {1,2,3,4,5,6,7,8,9,10}); Console.WriteLine ("D1: {0}", mc.Get ("D1"); Console.WriteLine ("D2: {0}", mc.Get ("D2") Console.WriteLine ("D3: {0}", mc.Get ("D3")); Console.WriteLine ("D4: {0}", mc.Get ("D4")); byte [] tmp = mc.Get ("D5"); / / delete them from the cache mc.Remove ("D1"); mc.Remove ("D2") Mc.Remove ("D3"); mc.Remove ("D4"); / / add an item which is valid for 10 mins mc.Store (StoreMode.Set, "D4", new Product (), new TimeSpan (0,10,0)); Console.WriteLine ("D4: {0}", mc.Get ("D4")); Console.ReadLine () } / / objects must be serializable to be able to store them in the cache [Serializable] class Product {public double Price = 1.24; public string Name = "Mineral Water"; public override string ToString () {return String.Format ("Product {{0}: {1}", this.Name, this.Price) }
Click F6 again and find 11 errors that say "the namespace of Enyim could not be found", but I have already added a reference to Enyim.Caching.
To solve this problem, open the properties of the project, change the project's "target framework" (from the original. NET Framework 4 Client Profile) to ".NET Framework 4", and then press F6 (build solution), and it is successful.
Press F5 (start debugging) and see the following results
(7) the above code does work, but if you are more demanding of yourself, you should write like this
Using (MemcachedClient client = new MemcachedClient ()) {/ / Store the record client.Store (StoreMode.Set, "currentTime", DateTime.Now.ToString ()); / / Retrieve the value string value = client.Get ("currentTime");}
Because the MemcachedClient class implements the IDisposable interface
Reference website: http://deanhume.com/home/blogpost/memcached-for-c----a-walkthrough/62
Similarly, in this article, the author also wrote a CacheLayer.cs file that a little wrapper the MemcachedClient, but there is one problem to note: the CacheLayer.cs class written by the author can only work under the 1.4.5 version of Northscale Memcached.
The code for CacheLayer.cs is as follows
Namespace MemcacheExample.Cache {using System; using Enyim.Caching; using Enyim.Caching.Memcached; public class CacheLayer {private static readonly MemcachedClient Cache = new MemcachedClient () / Retrieve cached item / Type of cached item / Name of cached item / Cached item as type public static T Get (string key) where T: class {try {return (T) Cache.Get (key) } catch {return null;}} / Insert value into the cache using / appropriate name/value pairs / Type of cached item / Item to be cached / Name of item / Duration of the cache. Public static void Add (T objectToCache, string key, int cacheDuration) where T: class {Cache.Store (StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes (cacheDuration));} / Insert value into the cache using / / appropriate name/value pairs / Item to be cached / / Name of item / Duration of the cache. Public static void Add (object objectToCache, string key, int cacheDuration) {Cache.Store (StoreMode.Set, key, objectToCache, DateTime.Now.AddMinutes (cacheDuration));} / Remove item from cache / Name of cached item public static void Remove (string key) {Cache.Remove (key) } / Clears all stored objects from memory. / public static void ClearAll () {Cache.FlushAll ();} / Check for item in cache / Name of cached item / A boolean if the object exists public static bool Exists (string key) {return Cache.Get (key)! = null } this is the end of the article on "how to access Memcached using Enyim.Caching". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.