Skip to content

Instantly share code, notes, and snippets.

@nickheppleston
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save nickheppleston/a414fb76eaa7a2dc27be to your computer and use it in GitHub Desktop.

Select an option

Save nickheppleston/a414fb76eaa7a2dc27be to your computer and use it in GitHub Desktop.

Revisions

  1. nickheppleston revised this gist Oct 13, 2014. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions AzureRedisCache-CacheSerializer.cs
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,9 @@
    * http://msdn.microsoft.com/en-us/library/azure/dn690524.aspx#Objects
    */

    using StackExchange.Redis;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;


    namespace AzureRedisCacheSample
    {
    public static class CacheSerializer
  2. nickheppleston renamed this gist Oct 13, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. nickheppleston revised this gist Oct 13, 2014. 1 changed file with 33 additions and 32 deletions.
    65 changes: 33 additions & 32 deletions CacheSerializer.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*
    * A simple Serialization Helper allowing custom .Net types to be stored
    * A simple generic Serialization Helper allowing custom .Net types to be stored
    * in the Azure Redis Cache. Inspired by the sample at:
    * http://msdn.microsoft.com/en-us/library/azure/dn690524.aspx#Objects
    */
    @@ -15,35 +15,36 @@

    namespace AzureRedisCacheSample
    {
    public static class CacheSerializer
    {
    public static byte[] Serialize(object o)
    {
    if (o == null)
    {
    return null;
    }

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream())
    {
    binaryFormatter.Serialize(memoryStream, o);
    byte[] objectDataAsStream = memoryStream.ToArray();
    return objectDataAsStream;
    }
    }

    public static T Deserialize<T>(byte[] stream)
    {
    if (stream == null)
    return (default(T));

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream(stream))
    {
    T result = (T)binaryFormatter.Deserialize(memoryStream);
    return result;
    }
    }
    }
    public static class CacheSerializer
    {
    public static byte[] Serialize(object o)
    {
    if (o == null)
    {
    return null;
    }

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream())
    {
    binaryFormatter.Serialize(memoryStream, o);
    byte[] objectDataAsStream = memoryStream.ToArray();
    return objectDataAsStream;
    }
    }

    public static T Deserialize<T>(byte[] stream)
    {
    if (stream == null)
    return (default(T));

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream(stream))
    {
    T result = (T)binaryFormatter.Deserialize(memoryStream);
    return result;
    }
    }
    }

    }
  4. nickheppleston created this gist Oct 13, 2014.
    49 changes: 49 additions & 0 deletions CacheSerializer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    /*
    * A simple Serialization Helper allowing custom .Net types to be stored
    * in the Azure Redis Cache. Inspired by the sample at:
    * http://msdn.microsoft.com/en-us/library/azure/dn690524.aspx#Objects
    */

    using StackExchange.Redis;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;

    namespace AzureRedisCacheSample
    {
    public static class CacheSerializer
    {
    public static byte[] Serialize(object o)
    {
    if (o == null)
    {
    return null;
    }

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream())
    {
    binaryFormatter.Serialize(memoryStream, o);
    byte[] objectDataAsStream = memoryStream.ToArray();
    return objectDataAsStream;
    }
    }

    public static T Deserialize<T>(byte[] stream)
    {
    if (stream == null)
    return (default(T));

    BinaryFormatter binaryFormatter = new BinaryFormatter();
    using (MemoryStream memoryStream = new MemoryStream(stream))
    {
    T result = (T)binaryFormatter.Deserialize(memoryStream);
    return result;
    }
    }
    }
    }