Headlines

  • We would focus on the key of the cache at first
  • Define the token
  • A complete example

 

 

 

We would focus on the key of the cache at first.

1. Simple type values

We can code the cache as following if the key of the cache is simple type.

Cache<long, SomeType> myCache = new Cache();

//add the key and the cached item

myCache.Add(someID, someTypeObj, cacheToken);

//read from the cache

var someTypeObj = myCache.Get(someID);

 

2. Complex type value

We should define the key as following first.

class MyKey : IEquatable<MyKey>

{

public string Name {get;set;}

public long ID{get;set}

public DateTime Time{get;set;}

public bool Equals(MyKey other)

{

return Name == other.Name && ID == other.ID && Time == other.Time;

}

}

 

Then define the cache.

Cache<MyKey, SomeType> cache = new Cache<MyKey, SomeType>();

//add the key and cached item

cache.Add(new MyKey{Name = “a”, ID = 23}, someTypeObj , cacheToken);

//read from the cache

var someTypeObj = cache.Get(new MyKey{Name= “a”, ID = 23});

 

Define the token

The token is the key object to define the lifetime of the cached item.

The requirement to the lifetime commonly goes into the following situation.

  1. The lifetime would be a time range, however, it should be of slide. Let’s say, the lifetime is 1 minute. The cached item would be expired and removed if it is not accessed within 1 minute.
  2. The lifetime would be a time range, however, it should be absolute. Let’s say, the lifetime is 1 minute. The cached item would be expired and removed after 1 minute.
  3. The lifetime would depend on the lifetime of other object.

In my opinion, #1 has the memory consideration, #2 has the business requirement consideration and #3 has the design consideration.

Let’s define the slide lifetime token.

class SlideToken : IToken

{

private int mSlimTimeInMinutes = 0;

private DateTime mExpirationTime ;

public SlideToken (int slimTimeInMinutes)
{
        mSlimTimeInMinutes = slimTimeInMinutes;
        mExpirationTime = DateTime.Now.AddMinutes(slimTimeInMinutes);
}

public bool IsCurrent
{
        get { return DateTime.Now <= mExpirationTime; }
}

public void Renew()
{
        mExpirationTime = DateTime.Now.AddMinutes(mSlimTimeInMinutes);
}

}

Let’s define the absolute lifetime token.

class AbsoluteToken : IToken

{

private DateTime mExpirationTime ;

public SlideToken (int absoluteTimeInMinutes)
{
        mExpirationTime = DateTime.Now.AddMinutes(absoluteTimeInMinutes);
}

public bool IsCurrent
{
        get { return DateTime.Now <= mExpirationTime; }
}

public void Renew()
{
        //do nothing

}

}

Let’s define the token depending on the lifetime of other object.

class ObjectToken : IToken

{

private WeakReference mTargetObject = null;

public ObjectToken(object targetObject)

{

    mTargetObject = new WeakReference (targetObject);

}

public bool IsCurrent

{

    var obj = mTargetObject.Target as object;

    return obj != null;

}

public void Renew()

{

//do nothing;

}

}

 

From the sample code above, I believe you must know how to define a token that will never expire.微笑

 

A complete example

   1:  //define the key
   2:  public ObjectKey : IEquatable<ObjectKey>
   3:  {
   4:      public long ID{get;set;}
   5:      public DateTime StartDate{get; set;}
   6:      public int Type {get; set;}
   7:      
   8:      public override bool Equals(ObjectKey other)
   9:      {
  10:          return ID == other.ID
  11:              && StartDate == other.StartDate
  12:              && Type == other.Type;
  13:      }
  14:  }
  15:   
  16:  //define the token
  17:  public class SlideToken : IToken
  18:  {
  19:      private int mLifetimeInMinutes;
  20:      private DateTime mExpiredTime;
  21:      public SlideToken(int lifetimeInMinutes)
  22:      {
  23:          mLifetimeInMinutes = lifetimeInMinutes;
  24:          mExpiredTime = DateTime.Now.AddMinutes(mLifeTimeInMinutes);
  25:      }
  26:   
  27:      public bool IsCurrent
  28:      {
  29:          return DateTime.Now <= mExpiredTime;
  30:      }
  31:   
  32:      public void Renew()
  33:      {
  34:          mExpiredTime = DateTime.Now.AddMinutes(mLifeTimeInMinutes);
  35:      }
  36:  }
  37:   
  38:  //define the cached item
  39:  public class CachedItem
  40:  {
  41:      public string Name{ get; set;}
  42:      public long RevID{ get; set;}
  43:      public int TypeID{ get; set;}
  44:  }
  45:   
  46:  //use the cache
  47:  Cache<ObjectKey, CachedItem> cache = new Cache<ObjectKey, CachedItem>();
  48:  cache.Add(new ObjectKey{
  49:      ID = 1,
  50:      StartDate = DateTime.Today,
  51:      Type = 0,
  52:  }, new CachedItem{}, new SlideToken(2));
  53:   
  54:  var cachedItem = cache.Get(new ObjectKey{
  55:      ID = 1, 
  56:      StartDate = DateTime.Today,
  57:      Type = 0});
  58:   
  59:  cachedItem = cache.GetWithCreate(new ObjectKey{
  60:      ID = 1, 
  61:      StartDate = DateTime.Today,
  62:      Type = 1
  63:  }, ()=>{
  64:  CachedItem item;
  65:  //try to get the object with web service request or from the Db
  66:  return item;
  67:  }, new SlideToken(2));
  68:   

Last edited Jul 26, 2011 at 1:31 AM by czy, version 6

Comments

No comments yet.