Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 794ae47

Browse files
authored
Merge pull request #2181 from github/feature/graphql-caching
GraphQL caching.
2 parents 1cf7713 + c8ace03 commit 794ae47

27 files changed

+2031
-88
lines changed

src/GitHub.Api/Caching/FileCache.cs

Lines changed: 1295 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)
3+
4+
This file is part of FileCache (http://github.com/acarteas/FileCache).
5+
6+
FileCache is distributed under the Apache License 2.0.
7+
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
8+
*/
9+
using System.Reflection;
10+
11+
namespace System.Runtime.Caching
12+
{
13+
/// <summary>
14+
/// You should be able to copy & paste this code into your local project to enable caching custom objects.
15+
/// </summary>
16+
public sealed class FileCacheBinder : System.Runtime.Serialization.SerializationBinder
17+
{
18+
public override Type BindToType(string assemblyName, string typeName)
19+
{
20+
Type typeToDeserialize = null;
21+
22+
String currentAssembly = Assembly.GetExecutingAssembly().FullName;
23+
24+
// In this case we are always using the current assembly
25+
assemblyName = currentAssembly;
26+
27+
// Get the type using the typeName and assemblyName
28+
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
29+
typeName, assemblyName));
30+
31+
return typeToDeserialize;
32+
}
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)
3+
4+
This file is part of FileCache (http://github.com/acarteas/FileCache).
5+
6+
FileCache is distributed under the Apache License 2.0.
7+
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
8+
*/
9+
10+
namespace System.Runtime.Caching
11+
{
12+
public class FileCacheEventArgs : EventArgs
13+
{
14+
public long CurrentCacheSize { get; private set; }
15+
public long MaxCacheSize { get; private set; }
16+
public FileCacheEventArgs(long currentSize, long maxSize)
17+
{
18+
CurrentCacheSize = currentSize;
19+
MaxCacheSize = maxSize;
20+
}
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)
3+
4+
This file is part of FileCache (http://github.com/acarteas/FileCache).
5+
6+
FileCache is distributed under the Apache License 2.0.
7+
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
8+
*/
9+
10+
namespace System.Runtime.Caching
11+
{
12+
[Serializable]
13+
public class FileCachePayload
14+
{
15+
public object Payload { get; set; }
16+
public SerializableCacheItemPolicy Policy { get; set; }
17+
18+
public FileCachePayload(object payload)
19+
{
20+
Payload = payload;
21+
Policy = new SerializableCacheItemPolicy()
22+
{
23+
AbsoluteExpiration = DateTime.Now.AddYears(10)
24+
};
25+
}
26+
27+
public FileCachePayload(object payload, SerializableCacheItemPolicy policy)
28+
{
29+
Payload = payload;
30+
Policy = policy;
31+
}
32+
}
33+
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)
3+
4+
This file is part of FileCache (http://github.com/acarteas/FileCache).
5+
6+
FileCache is distributed under the Apache License 2.0.
7+
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
8+
*/
9+
using System.Collections.Generic;
10+
11+
namespace System.Runtime.Caching
12+
{
13+
/// <summary>
14+
/// A basic min priorty queue (min heap)
15+
/// </summary>
16+
/// <typeparam name="T">Data type to store</typeparam>
17+
public class PriortyQueue<T> where T : IComparable<T>
18+
{
19+
20+
private List<T> _items;
21+
private IComparer<T> _comparer;
22+
23+
/// <summary>
24+
/// Default constructor.
25+
/// </summary>
26+
/// <param name="comparer">The comparer to use. The default comparer will make the smallest item the root of the heap.
27+
///
28+
/// </param>
29+
public PriortyQueue(IComparer<T> comparer = null)
30+
{
31+
_items = new List<T>();
32+
if (comparer == null)
33+
{
34+
_comparer = new GenericComparer<T>();
35+
}
36+
}
37+
38+
/// <summary>
39+
/// Constructor that will convert an existing list into a min heap
40+
/// </summary>
41+
/// <param name="unsorted">The unsorted list of items</param>
42+
/// <param name="comparer">The comparer to use. The default comparer will make the smallest item the root of the heap.</param>
43+
public PriortyQueue(List<T> unsorted, IComparer<T> comparer = null)
44+
: this(comparer)
45+
{
46+
for (int i = 0; i < unsorted.Count; i++)
47+
{
48+
_items.Add(unsorted[i]);
49+
}
50+
BuildHeap();
51+
}
52+
53+
private void BuildHeap()
54+
{
55+
for (int i = _items.Count / 2; i >= 0; i--)
56+
{
57+
adjustHeap(i);
58+
}
59+
}
60+
61+
//Percolates the item specified at by index down into its proper location within a heap. Used
62+
//for dequeue operations and array to heap conversions
63+
private void adjustHeap(int index)
64+
{
65+
//cannot percolate empty list
66+
if (_items.Count == 0)
67+
{
68+
return;
69+
}
70+
71+
//GOAL: get value at index, make sure this value is less than children
72+
// IF NOT: swap with smaller of two
73+
// (continue to do so until we can't swap)
74+
T item = _items[index];
75+
76+
//helps us figure out if a given index has children
77+
int end_location = _items.Count;
78+
79+
//keeps track of smallest index
80+
int smallest_index = index;
81+
82+
//while we're not the last thing in the heap
83+
while (index < end_location)
84+
{
85+
//get left child index
86+
int left_child_index = (2 * index) + 1;
87+
int right_child_index = left_child_index + 1;
88+
89+
//Three cases:
90+
// 1. left index is out of range
91+
// 2. right index is out or range
92+
// 3. both indices are valid
93+
if (left_child_index < end_location)
94+
{
95+
//CASE 1 is FALSE
96+
//remember that left index is the smallest
97+
smallest_index = left_child_index;
98+
99+
if (right_child_index < end_location)
100+
{
101+
//CASE 2 is FALSE (CASE 3 is true)
102+
//TODO: find value of smallest index
103+
smallest_index = (_comparer.Compare(_items[left_child_index], _items[right_child_index]) < 0)
104+
? left_child_index
105+
: right_child_index;
106+
}
107+
}
108+
109+
//we have two things: original index and (potentially) a child index
110+
if (_comparer.Compare(_items[index], _items[smallest_index]) > 0)
111+
{
112+
//move parent down (it was too big)
113+
T temp = _items[index];
114+
_items[index] = _items[smallest_index];
115+
_items[smallest_index] = temp;
116+
117+
//update index
118+
index = smallest_index;
119+
}
120+
else
121+
{
122+
//no swap necessary
123+
break;
124+
}
125+
}
126+
}
127+
128+
public bool isEmpty()
129+
{
130+
return _items.Count == 0;
131+
}
132+
133+
public int GetSize()
134+
{
135+
return _items.Count;
136+
}
137+
138+
139+
public void Enqueue(T item)
140+
{
141+
//calculate positions
142+
int current_position = _items.Count;
143+
int parent_position = (current_position - 1) / 2;
144+
145+
//insert element (note: may get erased if we hit the WHILE loop)
146+
_items.Add(item);
147+
148+
//find parent, but be careful if we are an empty queue
149+
T parent = default(T);
150+
if (parent_position >= 0)
151+
{
152+
//find parent
153+
parent = _items[parent_position];
154+
155+
//bubble up until we're done
156+
while (_comparer.Compare(parent, item) > 0 && current_position > 0)
157+
{
158+
//move parent down
159+
_items[current_position] = parent;
160+
161+
//recalculate position
162+
current_position = parent_position;
163+
parent_position = (current_position - 1) / 2;
164+
165+
//make sure that we have a valid index
166+
if (parent_position >= 0)
167+
{
168+
//find parent
169+
parent = _items[parent_position];
170+
}
171+
}
172+
} //end check for nullptr
173+
174+
//after WHILE loop, current_position will point to the place that
175+
//variable "item" needs to go
176+
_items[current_position] = item;
177+
178+
}
179+
180+
public T GetFirst()
181+
{
182+
return _items[0];
183+
}
184+
185+
public T Dequeue()
186+
{
187+
int last_position = _items.Count - 1;
188+
T last_item = _items[last_position];
189+
T top = _items[0];
190+
_items[0] = last_item;
191+
_items.RemoveAt(_items.Count - 1);
192+
193+
//percolate down
194+
adjustHeap(0);
195+
return top;
196+
}
197+
198+
199+
private class GenericComparer<TInner> : IComparer<TInner> where TInner : IComparable<TInner>
200+
{
201+
public int Compare(TInner x, TInner y)
202+
{
203+
return x.CompareTo(y);
204+
}
205+
}
206+
}
207+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)
3+
4+
This file is part of FileCache (http://github.com/acarteas/FileCache).
5+
6+
FileCache is distributed under the Apache License 2.0.
7+
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
8+
*/
9+
10+
namespace System.Runtime.Caching
11+
{
12+
[Serializable]
13+
public class SerializableCacheItemPolicy
14+
{
15+
public DateTimeOffset AbsoluteExpiration { get; set; }
16+
17+
private TimeSpan _slidingExpiration;
18+
public TimeSpan SlidingExpiration
19+
{
20+
get
21+
{
22+
return _slidingExpiration;
23+
}
24+
set
25+
{
26+
_slidingExpiration = value;
27+
if (_slidingExpiration > new TimeSpan())
28+
{
29+
AbsoluteExpiration = DateTimeOffset.Now.Add(_slidingExpiration);
30+
}
31+
}
32+
}
33+
public SerializableCacheItemPolicy(CacheItemPolicy policy)
34+
{
35+
AbsoluteExpiration = policy.AbsoluteExpiration;
36+
SlidingExpiration = policy.SlidingExpiration;
37+
}
38+
39+
public SerializableCacheItemPolicy()
40+
{
41+
SlidingExpiration = new TimeSpan();
42+
}
43+
}
44+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<ItemGroup>
1818
<Reference Include="System.ComponentModel.Composition" />
1919
<Reference Include="System.Net.Http" />
20+
<Reference Include="System.Runtime.Caching" />
2021
</ItemGroup>
2122

2223
<ItemGroup>

0 commit comments

Comments
 (0)