Repository navigation
Expand file tree
/
Copy pathCache.cs
More file actions
202 lines (132 loc) · 5.47 KB
/
Copy pathCache.cs
File metadata and controls
202 lines (132 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using Microsoft.Data.Sqlite;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.ComponentModel.Design.ObjectSelectorEditor;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SteamProxyLanCache
{
public class Cache
{
public static string cs = "Data Source=cache.db;Cache=Shared;Pooling=True;Mode=ReadWriteCreate";
//public static string cs = "Data Source=cache;Cache=Shared;Pooling=True;Mode=Memory";
public SqliteConnection conn;
public static List<string> filecache;
public string sDateUsage = "";
public static string sDateFormat = "yyyyMMdd";
public Cache()
{
filecache = new List<string> { };
using (conn = new SqliteConnection(cs))
{
conn.Open();
var command = conn.CreateCommand();
command.CommandText = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'cache'";
using (var reader = command.ExecuteReader())
{
if (!reader.HasRows)
{
string cs = "CREATE TABLE cache(name CHAR(254) PRIMARY KEY NOT NULL,accessed CHAR(24) NOT NULL)";
command = conn.CreateCommand();
command.CommandText = cs;
command.ExecuteNonQuery();
}
}
command = conn.CreateCommand();
command.CommandText = "SELECT name,accessed FROM cache";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
filecache.Add(reader.GetString(0) + "-" + reader.GetString(1));
}
}
}
}
public void RegisterFile(string name)
{
sDateUsage = DateTime.Now.ToString(sDateFormat);
if(!filecache.Contains(name + "-" + sDateUsage))
{
filecache.Add(name + "-" + sDateUsage);
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
}
int iRows = -1;
var command = conn.CreateCommand();
command.CommandText = "INSERT OR REPLACE INTO cache(name,accessed)VALUES('" + name + "','" + sDateUsage + "')";
iRows = command.ExecuteNonQuery();
}
}
public int CleanCacheDBFiles(int days, string cachefilepath)
{
int filesDeleted = 0;
if (days > 0) { days = days * -1; }
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
}
//first clean the files which are in database
string sql = "SELECT * FROM cache WHERE accessed <= '" + DateAndTime.Now.AddDays(days).ToString("yyyyMMdd") + "'";
var command = conn.CreateCommand();
command.CommandText = sql;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string filename = cachefilepath + "\\" + reader.GetString(0) + ".dat";
DateTime fileaccessed = DateTime.ParseExact(reader.GetString(1), sDateFormat, new CultureInfo("nl-BE"));
if(fileaccessed <= DateAndTime.Now.AddDays(days)){
if (File.Exists(filename))
{
File.Delete(filename);
}
filesDeleted++;
}
}
}
sql = "DELETE FROM cache WHERE accessed <= '" + DateAndTime.Now.AddDays(days).ToString("yyyyMMdd") + "'";
command = conn.CreateCommand();
command.CommandText = sql;
command.ExecuteNonQuery();
return filesDeleted;
}
public int CleanCacheDiskFiles(int days, string cachefilepath)
{
int filesDeleted = 0;
if (days > 0) { days = days * -1; }
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
}
//second loop through files on disk and see if they are in database, if not, delete the file;
string[] files = Directory.GetFiles(cachefilepath, "*.dat", SearchOption.AllDirectories);
foreach (string sFile in files)
{
string sFileName = sFile.Replace(cachefilepath, "");
sFileName = sFileName.Replace(".dat", "");
sFileName = sFileName.Replace("\\", "/");
string sql = "SELECT * FROM cache WHERE name = '" + sFileName + "'";
var command = conn.CreateCommand();
command.CommandText = sql;
var reader = command.ExecuteReader();
if (!reader.HasRows)
{
File.Delete(sFile);
filesDeleted++;
}
}
/*sql = "VACUUM cache.db";
command = conn.CreateCommand();
command.CommandText = sql;
command.ExecuteNonQuery();*/
return filesDeleted;
}
}
}