2

現在、私はこれを行っています

try
{
    cloudFilesProvider.CreateObjectFromFile(inStrContainerName, inStrSrcFilePath, strDesFileName, 4096, FileMetaData);

}
catch (ItemNotFoundException ex1)
{
     try
     {
         cloudFilesProvider.CreateContainer(inStrContainerName);
         cloudFilesProvider.CreateObjectFromFile(inStrContainerName, inStrSrcFilePath, strDesFileName, 4096, FileMetaData);
     }
     catch(Exception ex2)
     {
         return false;
     }
}

基本的に、コンテナが存在しない場合は、3 つの個別の API 呼び出しが必要です。

これを行うより効率的な方法はありますか?

4

1 に答える 1

4

コードを次の 2 行に減らすことで、コードを簡素化できます。

cloudFilesProvider.CreateContainer(inStrContainerName);
cloudFilesProvider.CreateObjectFromFile(inStrContainerName, inStrSrcFilePath, strDesFileName, 4096, FileMetaData);

CreateContainerすでに存在するコンテナを呼び出しても安全です。ContainerCreatedコンテナーが作成された場合、またはContainerExists既に存在するためにコンテナーが作成されなかった場合に返されます。

PS: メソッドの戻り値 (上記のような情報を含む) は、IObjectStorageProviderリリース 1.2.0.0 で十分に文書化されます。


編集: コードで行う API 呼び出しの数を減らすには、次のようなキャッシュ クラスを使用できます。このCreateIfNecessaryメソッドは、コンテナーが存在することが事前にわかっていない場合にのみ、コンテナーの作成を試みます。このClearCacheメソッドは、コンテナーを削除してもキャッシュを引き続き使用するための手動の方法を提供します。

このコードは現在テストされていません。

public class ContainerManager
{
    private readonly CloudFilesProvider _provider;
    private readonly string _region;
    private readonly bool _useInternalUrl;
    private readonly CloudIdentity _identity;

    private readonly HashSet<string> _containers = new HashSet<string>();

    public ContainerManager(CloudFilesProvider provider, string region, bool useInternalUrl, CloudIdentity identity)
    {
        if (provider == null)
            throw new ArgumentNullException("provider");

        _provider = provider;
        _region = region;
        _useInternalUrl = useInternalUrl;
        _identity = identity;
    }

    /// <summary>
    /// Clears the cache of known containers.
    /// </summary>
    /// <remarks>
    /// <alert class="warning">
    /// If a container was deleted after this cache was in use, this method must be called or
    /// <see cref="CreateIfNecessary(string)"/> could fail to create a container which does not
    /// exist.
    /// </alert>
    /// </remarks>
    public void ClearCache()
    {
        lock (_containers)
        {
            _containers.Clear();
        }
    }

    /// <summary>
    /// Ensures that a container exists in the Cloud Files store.
    /// </summary>
    /// <remarks>
    /// <alert class="warning">
    /// If a container was deleted after this cache was in use, and <see cref="ClearCache()"/>
    /// has not been called, this method could fail to create a container which does not exist.
    /// </alert>
    /// </remarks>
    /// <param name="container">The name of the container to create.</param>
    /// <exception cref="ArgumentNullException">If <paramref name="container"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentException">If <paramref name="container"/> is empty.</exception>
    /// <returns><c>true</c> if the container was created; otherwise <c>false</c> if the container already existed.</returns>
    public bool CreateIfNecessary(string container)
    {
        if (container == null)
            throw new ArgumentNullException("container");
        if (string.IsNullOrEmpty(container))
            throw new ArgumentException("container cannot be empty");

        // don't try to create the same container multiple times
        if (_containers.Contains(container))
            return false;

        ObjectStore result = _provider.CreateContainer(container, _region, _useInternalUrl, _identity);
        if (result == ObjectStore.ContainerCreated || result == ObjectStore.ContainerExists)
        {
            lock (_containers)
            {
                // add to _containers even if the result is ContainerExists, because that
                // means it simply wasn't known to this cache.
                _containers.Add(container);
            }
        }

        // only return true if the container was actually created
        return result == ObjectStore.ContainerCreated;
    }
}
于 2013-08-05T23:21:22.923 に答える