Showing posts with label memcache. Show all posts
Showing posts with label memcache. Show all posts

Tuesday, 31 January 2012

MemCached vs Redis for ASP.NET and Entity framework

memcached and redis differ quite a bit.
Memcached is completely in memory and will lose all of its cache in case the server is restarted.
Redis is persistent, and on top of that has a lot more features (like set operations, lists, counters, etc).
Since you're only talking about a cache, memcache might be the better fit since it's dedicated for just that.
We use memcache for a lot of things and what it does it does good. If it turns out later that you need persistence, you can always switch for memcachedb which uses the same mecache protocol but has a berkelydb backend

MemcacheProvider For C#


using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;

namespace CafeX.Core.Provider
{
    public class MemcacheProvider
    {
        private static MemcachedClient _instant;
        public static MemcachedClient Instant
        {
            get { return _instant ?? (_instant = new MemcachedClient()); }
        }

        public static object Get(string key)
        {
            return Instant.Get(key);
        }

        public static void Add(string key, object value)
        {
           Instant.Store(StoreMode.Add, key, value);
        }

        public static void AddWithTimeOut(string key, object value, int timeout)
        {
            Instant.Store(StoreMode.Add, key, value, DateTime.Now.AddMinutes(timeout));
        }

        public static void Update(string key, object value)
        {
            Instant.Store(StoreMode.Set, key, value);
        }

        public static void UpdateWithTimeOut(string key, object value, int timeout)
        {
            Instant.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(timeout));
        }

        public static void Remove(string key)
        {
            Instant.Remove(key);
        }
    }
}

Provider for Memcache(VB.NET)


Imports Enyim.Caching
Imports Enyim.Caching.Memcached

Namespace CacheService
    Public Class MemcacheProvider
        Implements ICacheProvider

        Private Shared _instant As MemcachedClient
        Public Shared ReadOnly Property Instant() As MemcachedClient
            Get
                If _instant Is Nothing Then
                    _instant = New MemcachedClient()
                End If
                Return _instant
            End Get
        End Property

        Public Function [Get](ByVal key As String) As Object Implements ICacheProvider.[Get]
            Return Instant.[Get](key)
        End Function

        Public Sub Add(ByVal key As String, ByVal value As Object) Implements ICacheProvider.Add
            Instant.Store(StoreMode.Add, key, value)
        End Sub

        Public Sub Add(ByVal key As String, ByVal value As Object, ByVal timeout As Integer) Implements ICacheProvider.Add
            Instant.Store(StoreMode.Add, key, value, DateTime.Now.AddMinutes(timeout))
        End Sub

        Public Sub Update(ByVal key As String, ByVal value As Object) Implements ICacheProvider.Update
            Instant.Store(StoreMode.[Set], key, value)
        End Sub

        Public Sub UpdateWithTimeOut(ByVal key As String, ByVal value As Object, ByVal timeout As Integer) Implements ICacheProvider.Update
            Instant.Store(StoreMode.[Set], key, value, DateTime.Now.AddMinutes(timeout))
        End Sub

        Public Sub Remove(ByVal key As String) Implements ICacheProvider.Remove
            Instant.Remove(key)
        End Sub


    End Class
End Namespace

Monday, 30 January 2012

memcached for Windows


memcached for Windows

This is a port of memcached to the win32 architecture by Kenneth Dalgleish, based on Kronuz's 1.2.1 port. This port is not supported by the official memcached team.

Install

The win32 version of memcached can be run both as a NT Service or from the command line. To install memcached as a service, follow the next steps:
  1. Unzip the binaries in your desired directory (eg. c:\memcached)
  2. Install the service using the command: 'c:\memcached\memcached.exe -d install' from the command line
  3. Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'
  4. Use the server, by default listening to port 11211

Building from source

To build from source, you will need Visual Studio 2005 (any edition with C++ should work), Windows SDK (eg.Windows SDK for Windows Server 2008 and .NET Framework 3.5) and libevent (win32 binary provided on this page).
  1. Install Visual Studio 2005
  2. Install Windows SDK
  3. Put libevent.lib in Win32-Prj/ folder
  4. Open solution file and it should build

Downloads

memcached 1.2.4 Win32 Beta

Libevent 1.3e Win32

(Needed if building from source)