新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 基于云存储实现用Windows Azure Storage增强应用程序的引擎

基于云存储实现用Windows Azure Storage增强应用程序的引擎

作者:时间:2018-09-11来源:网络收藏

本文引用地址:http://www.eepw.com.cn/article/201809/388914.htm

  现在,我们需要一个简单的包装来与我们的队列交互。从本质上说,我们需要能够将消息插入队列,获取任何挂起的消息并清除该队列(请参见图 3)。

  图 3 用于与队列交互的包装

namespace HollywoodHackers..Queue 
{ 
  public class StdQueueT> : 
  Base where T : QueueMessageBase, new() 
  { 
    protected CloudQueue queue; 
    protected CloudQueueClient client; 
 
    public StdQueue(string queueName) 
    { 
      client = new CloudQueueClient 
      (Base.QueueBaseUri, StorageBase.Credentials); 
      queue = client.GetQueueReference(queueName); 
      queue.CreateIfNotExist(); 
    } 
    public void AddMessage(T message) 
    { 
      CloudQueueMessage msg = 
      new CloudQueueMessage(message.ToBinary()); 
      queue.AddMessage(msg); 
    } 
    public void DeleteMessage(CloudQueueMessage msg) 
    { 
      queue.DeleteMessage(msg); 
    } 
    public CloudQueueMessage GetMessage() 
    { 
      return queue.GetMessage(TimeSpan.FromSeconds(60)); 
    } 
  } 
  public class ToastQueue : StdQueueToastQueueMessage> 
  { 
    public ToastQueue() 
      : base(toasts) 
    { 
    } 
  } 
}

我们还需要为表存储设置一个包装,以便在用户登录到站点之前可以存储用户通知。可以使用 PartitionKey(行集合的标识符)和 RowKey(可唯一标识特定分区中的每个单独行)组织表数据。选择 PartitionKey 和 RowKey 使用的数据是在使用表存储时所做的最重要的设计决策之一。

  这些特点允许跨存储节点进行负载平衡,并在应用程序中提供内置的可伸缩性选项。不考虑数据的数据中心关联性,使用同一分区键的表存储中的行将保留在相同的物理数据存储中。因为针对每个用户存储对应的消息,所以分区键将是 UserName,而 RowKey 则成为标识每行的 GUID(请参见图 4)。

  图 4 表存储的包装

namespace HollywoodHackers.Storage.Repositories 
{ 
  public class UserTextNotificationRepository : StorageBase 
  { 
    public const string EntitySetName = 
    UserTextNotifications; 
    CloudTableClient tableClient; 
    UserTextNotificationContext notificationContext; 
    public UserTextNotificationRepository() 
      : base() 
    { 
      tableClient = new CloudTableClient 
      (StorageBase.TableBaseUri, StorageBase.Credentials); 
      notificationContext = new UserTextNotificationContext 
      (StorageBase.TableBaseUri,StorageBase.Credentials); 
 
      tableClient.CreateTableIfNotExist(EntitySetName); 
    } 
    public UserTextNotification[] 
    GetNotificationsForUser(string userName) 
    { 
      var q = from notification in 
          notificationContext.UserNotifications 
          where notification.TargetUserName == 
          userName select notification; 
      return q.ToArray(); 
    } 
    public void AddNotification 
    (UserTextNotification notification) 
    { 
      notification.RowKey = Guid.NewGuid().ToString(); 
      notificationContext.AddObject 
      (EntitySetName, notification); 
      notificationContext.SaveChanges(); 
    } 
  } 
}



评论


相关推荐

技术专区