ftp缓存信息

using System.Collections.Generic;
using NewTempo.Ftp;
using System.IO;
using NshowAdClient.NshowAdServices;
using NshowAdClient.Helper;

namespace NshowAdClient.Services
{
    public static class
        FtpHelpService
    {
        static readonly Dictionary<int, FtpInfo> ClientFtpCache = new Dictionary<int, FtpInfo>();

        static FtpInfo ShareFtpInfo { get; set; }

        static readonly object GetShareFtpLock = new object();
        public static MyFtp GetShareFtp()
        {
            lock (GetShareFtpLock)
            {
                if (LoginUserHelper.Mode == RoleType.Client)
                {
                    return GetClientFtp(LoginUserHelper.UserId);
                }

                if (ShareFtpInfo == null)
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        ShareFtpInfo = service.GetFtpInfo();
                    }
                }

                MyFtp ftp = new MyFtp(ShareFtpInfo.FtpAddress, ShareFtpInfo.User, ShareFtpInfo.Password);
                return ftp;
                //return new MyFtp("61.145.119.85:21", "wcf", "wcf");
            }
        }

        static readonly object GetClientFtpLock = new object();
        public static MyFtp GetClientFtp(int clientId)
        {
            lock (GetClientFtpLock)
            {
                FtpInfo ftpInfo;
                if (ClientFtpCache.ContainsKey(clientId))
                {
                    ftpInfo = ClientFtpCache[clientId];
                }
                else
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        ftpInfo = service.GetClientFtpInfo(clientId);
                        ClientFtpCache.Add(clientId, ftpInfo);
                    }
                }

                var ftp = new MyFtp(ftpInfo.FtpAddress, ftpInfo.User, ftpInfo.Password);
                return ftp;
                //return new MyFtp("61.145.119.85:21", "wcf", "wcf");
            }
        }

        public static MyFtp GetFtp(string ftpIp, string userName, string password)
        {
            return new MyFtp(ftpIp, userName, password);
        }

        public static void CreateDir(string path, MyFtp myFtp)
        {
            var dirName = Path.GetDirectoryName(path);
            bool ret = myFtp.CreateDirectory(dirName);
        }

        public static FtpInfo GetClientFtpInfo(int clientId)
        {
            lock (GetClientFtpLock)
            {
                if (ClientFtpCache.ContainsKey(clientId))
                {
                    return ClientFtpCache[clientId];
                }
                else
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        FtpInfo ftpInfo = service.GetClientFtpInfo(clientId);
                        ClientFtpCache.Add(clientId, ftpInfo);
                        return ftpInfo;
                    }
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/wywnet/p/4755215.html