Click here to Skip to main content
15,896,201 members
Articles / Hosted Services / Azure

Using Windows Azure AppFabric Cache (CTP)

Rate me:
Please Sign up or sign in to vote.
4.96/5 (12 votes)
16 Feb 2011CPOL6 min read 58.9K   20  
How to setup and use Windows Azure AppFabric Cache (CTP).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationServer.Caching;

namespace AppFabricDataCache
{
    class Program
    {
        static void Main(string[] args)
        {
            // DataCacheFactory will use settings from app.config
            using (DataCacheFactory dataCacheFactory = new DataCacheFactory())
            {
                // Get a default cache client
                DataCache dataCache = dataCacheFactory.GetDefaultCache();

                // Create a new person
                Person nPerson = new Person("Panagiotis", "Kefalidis");
                Console.WriteLine(string.Format("Currently in your account: {0}", nPerson.GetBalance()));

                // Deposit some money
                nPerson.DepositMoney(100);
                Console.WriteLine(string.Format("Currently in your account after deposit: {0}", nPerson.GetBalance()));

                // Put that object into the cache
                dataCache.Put(nPerson.GetFullName(), nPerson);

                // Remove some money
                nPerson.WithdrawMoney(50);

                // Get the cached object back from the cache
                Person cachedPerson = (Person)dataCache.Get(nPerson.GetFullName());
                
                // How much money do we have now?
                Console.WriteLine(string.Format("Currently in your cached account after withdraw: {0}", cachedPerson.GetBalance()));

                // How much money we REALLY have now?
                Console.WriteLine(string.Format("Currently in your account after withdraw: {0}", nPerson.GetBalance()));

                // Update the cache with latest information
                dataCache.Put(nPerson.GetFullName(), nPerson);
            }
            Console.ReadKey();

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer
Belgium Belgium
Panagiotis is a Software Development Consultant & Architect and Trainer. He has been a speaker at domestic and international events such TechEd and ITProDevConnections, delivering trainings and consulting on cloud computing and the Windows Azure Platform for almost 3 years. He also has experience with other cloud platforms like AWS, Google Gears and others. He's a supporter of the EuroCloud movement, likes Pizza, Sushi and fast cars. Born and raised in Greece but currently living and working at Belgium for Devoteam BE.

Panagiotis is a Windows Azure MVP.

Comments and Discussions