Skip to content

Commit

Permalink
using LRUCache keep it simple
Browse files Browse the repository at this point in the history
  • Loading branch information
AkaneAkaza committed May 30, 2018
1 parent 873b9be commit f542b21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
50 changes: 13 additions & 37 deletions shadowsocks-csharp/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ public class Configuration
public Dictionary<string, PortMapConfig> portMap = new Dictionary<string, PortMapConfig>();

private Dictionary<int, ServerSelectStrategy> serverStrategyMap = new Dictionary<int, ServerSelectStrategy>();
private Dictionary<string, UriVisitTime> uri2time = new Dictionary<string, UriVisitTime>();
private SortedDictionary<UriVisitTime, string> time2uri = new SortedDictionary<UriVisitTime, string>();
private Dictionary<int, PortMapConfigCache> portMapCache = new Dictionary<int, PortMapConfigCache>();
private LRUCache<string, UriVisitTime> uricache = new LRUCache<string, UriVisitTime>(180);

private static string CONFIG_FILE = "gui-config.json";

Expand All @@ -174,9 +173,9 @@ public bool KeepCurrentServer(int localPort, string targetAddr, string id)
serverStrategyMap[localPort] = new ServerSelectStrategy();
ServerSelectStrategy serverStrategy = serverStrategyMap[localPort];

if (uri2time.ContainsKey(targetAddr))
if (uricache.ContainsKey(targetAddr))
{
UriVisitTime visit = uri2time[targetAddr];
UriVisitTime visit = uricache.Get(targetAddr);
int index = -1;
for (int i = 0; i < configs.Count; ++i)
{
Expand All @@ -188,11 +187,7 @@ public bool KeepCurrentServer(int localPort, string targetAddr, string id)
}
if (index >= 0 && visit.index == index && configs[index].enable)
{
time2uri.Remove(visit);
visit.index = index;
visit.visitTime = DateTime.Now;
uri2time[targetAddr] = visit;
time2uri[visit] = targetAddr;
uricache.Del(targetAddr);
return true;
}
}
Expand All @@ -209,25 +204,14 @@ public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc fi
serverStrategyMap[localPort] = new ServerSelectStrategy();
ServerSelectStrategy serverStrategy = serverStrategyMap[localPort];

foreach (KeyValuePair<UriVisitTime, string> p in time2uri)
uricache.SetTimeout(keepVisitTime);
uricache.Sweep();
if (sameHostForSameTarget && !forceRandom && targetAddr != null && uricache.ContainsKey(targetAddr))
{
if ((DateTime.Now - p.Key.visitTime).TotalSeconds < keepVisitTime)
break;

uri2time.Remove(p.Value);
time2uri.Remove(p.Key);
break;
}
if (sameHostForSameTarget && !forceRandom && targetAddr != null && uri2time.ContainsKey(targetAddr))
{
UriVisitTime visit = uri2time[targetAddr];
UriVisitTime visit = uricache.Get(targetAddr);
if (visit.index < configs.Count && configs[visit.index].enable && configs[visit.index].ServerSpeedLog().ErrorContinurousTimes == 0)
{
//uri2time.Remove(targetURI);
time2uri.Remove(visit);
visit.visitTime = DateTime.Now;
uri2time[targetAddr] = visit;
time2uri[visit] = targetAddr;
uricache.Del(targetAddr);
return configs[visit.index];
}
}
Expand Down Expand Up @@ -273,12 +257,7 @@ public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc fi
visit.uri = targetAddr;
visit.index = index;
visit.visitTime = DateTime.Now;
if (uri2time.ContainsKey(targetAddr))
{
time2uri.Remove(uri2time[targetAddr]);
}
uri2time[targetAddr] = visit;
time2uri[visit] = targetAddr;
uricache.Set(targetAddr, visit);
}
return configs[index];
}
Expand Down Expand Up @@ -308,12 +287,7 @@ public Server GetCurrentServer(int localPort, ServerSelectStrategy.FilterFunc fi
visit.uri = targetAddr;
visit.index = selIndex;
visit.visitTime = DateTime.Now;
if (uri2time.ContainsKey(targetAddr))
{
time2uri.Remove(uri2time[targetAddr]);
}
uri2time[targetAddr] = visit;
time2uri[visit] = targetAddr;
uricache.Set(targetAddr, visit);
}
return configs[selIndex];
}
Expand Down Expand Up @@ -380,6 +354,8 @@ public void FlushPortMapCache()
if (!portMapCache.ContainsKey(localPort))
serverStrategyMap.Remove(localPort);
}

uricache.Clear();
}

public Dictionary<int, PortMapConfigCache> GetPortMapCache()
Expand Down
10 changes: 10 additions & 0 deletions shadowsocks-csharp/Model/LRUCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public void SetTimeout(int time)
_sweep_time = time;
}

public void Clear()
{
lock (_lock)
{
_store.Clear();
_key_2_time.Clear();
_time_2_key.Clear();
}
}

public bool isTimeout(K key)
{
lock (_lock)
Expand Down

0 comments on commit f542b21

Please sign in to comment.