Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,295 changes: 1,295 additions & 0 deletions src/GitHub.Api/Caching/FileCache.cs

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions src/GitHub.Api/Caching/FileCacheBinder.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
/*
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)

This file is part of FileCache (http://github.com/acarteas/FileCache).

FileCache is distributed under the Apache License 2.0.
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
*/
using System.Reflection;

namespace System.Runtime.Caching
{
/// <summary>
/// You should be able to copy & paste this code into your local project to enable caching custom objects.
/// </summary>
public sealed class FileCacheBinder : System.Runtime.Serialization.SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;

String currentAssembly = Assembly.GetExecutingAssembly().FullName;

// In this case we are always using the current assembly
assemblyName = currentAssembly;

// Get the type using the typeName and assemblyName
typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));

return typeToDeserialize;
}
}
}
22 changes: 22 additions & 0 deletions src/GitHub.Api/Caching/FileCacheEventArgs.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
/*
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)

This file is part of FileCache (http://github.com/acarteas/FileCache).

FileCache is distributed under the Apache License 2.0.
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
*/

namespace System.Runtime.Caching
{
public class FileCacheEventArgs : EventArgs
{
public long CurrentCacheSize { get; private set; }
public long MaxCacheSize { get; private set; }
public FileCacheEventArgs(long currentSize, long maxSize)
{
CurrentCacheSize = currentSize;
MaxCacheSize = maxSize;
}
}
}
33 changes: 33 additions & 0 deletions src/GitHub.Api/Caching/FileCachePayload.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
/*
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)

This file is part of FileCache (http://github.com/acarteas/FileCache).

FileCache is distributed under the Apache License 2.0.
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
*/

namespace System.Runtime.Caching
{
[Serializable]
public class FileCachePayload
{
public object Payload { get; set; }
public SerializableCacheItemPolicy Policy { get; set; }

public FileCachePayload(object payload)
{
Payload = payload;
Policy = new SerializableCacheItemPolicy()
{
AbsoluteExpiration = DateTime.Now.AddYears(10)
};
}

public FileCachePayload(object payload, SerializableCacheItemPolicy policy)
{
Payload = payload;
Policy = policy;
}
}
}
207 changes: 207 additions & 0 deletions src/GitHub.Api/Caching/PriortyQueue.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
/*
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)

This file is part of FileCache (http://github.com/acarteas/FileCache).

FileCache is distributed under the Apache License 2.0.
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
*/
using System.Collections.Generic;

namespace System.Runtime.Caching
{
/// <summary>
/// A basic min priorty queue (min heap)
/// </summary>
/// <typeparam name="T">Data type to store</typeparam>
public class PriortyQueue<T> where T : IComparable<T>
{

private List<T> _items;
private IComparer<T> _comparer;

/// <summary>
/// Default constructor.
/// </summary>
/// <param name="comparer">The comparer to use. The default comparer will make the smallest item the root of the heap.
///
/// </param>
public PriortyQueue(IComparer<T> comparer = null)
{
_items = new List<T>();
if (comparer == null)
{
_comparer = new GenericComparer<T>();
}
}

/// <summary>
/// Constructor that will convert an existing list into a min heap
/// </summary>
/// <param name="unsorted">The unsorted list of items</param>
/// <param name="comparer">The comparer to use. The default comparer will make the smallest item the root of the heap.</param>
public PriortyQueue(List<T> unsorted, IComparer<T> comparer = null)
: this(comparer)
{
for (int i = 0; i < unsorted.Count; i++)
{
_items.Add(unsorted[i]);
}
BuildHeap();
}

private void BuildHeap()
{
for (int i = _items.Count / 2; i >= 0; i--)
{
adjustHeap(i);
}
}

//Percolates the item specified at by index down into its proper location within a heap. Used
//for dequeue operations and array to heap conversions
private void adjustHeap(int index)
{
//cannot percolate empty list
if (_items.Count == 0)
{
return;
}

//GOAL: get value at index, make sure this value is less than children
// IF NOT: swap with smaller of two
// (continue to do so until we can't swap)
T item = _items[index];

//helps us figure out if a given index has children
int end_location = _items.Count;

//keeps track of smallest index
int smallest_index = index;

//while we're not the last thing in the heap
while (index < end_location)
{
//get left child index
int left_child_index = (2 * index) + 1;
int right_child_index = left_child_index + 1;

//Three cases:
// 1. left index is out of range
// 2. right index is out or range
// 3. both indices are valid
if (left_child_index < end_location)
{
//CASE 1 is FALSE
//remember that left index is the smallest
smallest_index = left_child_index;

if (right_child_index < end_location)
{
//CASE 2 is FALSE (CASE 3 is true)
//TODO: find value of smallest index
smallest_index = (_comparer.Compare(_items[left_child_index], _items[right_child_index]) < 0)
? left_child_index
: right_child_index;
}
}

//we have two things: original index and (potentially) a child index
if (_comparer.Compare(_items[index], _items[smallest_index]) > 0)
{
//move parent down (it was too big)
T temp = _items[index];
_items[index] = _items[smallest_index];
_items[smallest_index] = temp;

//update index
index = smallest_index;
}
else
{
//no swap necessary
break;
}
}
}

public bool isEmpty()
{
return _items.Count == 0;
}

public int GetSize()
{
return _items.Count;
}


public void Enqueue(T item)
{
//calculate positions
int current_position = _items.Count;
int parent_position = (current_position - 1) / 2;

//insert element (note: may get erased if we hit the WHILE loop)
_items.Add(item);

//find parent, but be careful if we are an empty queue
T parent = default(T);
if (parent_position >= 0)
{
//find parent
parent = _items[parent_position];

//bubble up until we're done
while (_comparer.Compare(parent, item) > 0 && current_position > 0)
{
//move parent down
_items[current_position] = parent;

//recalculate position
current_position = parent_position;
parent_position = (current_position - 1) / 2;

//make sure that we have a valid index
if (parent_position >= 0)
{
//find parent
parent = _items[parent_position];
}
}
} //end check for nullptr

//after WHILE loop, current_position will point to the place that
//variable "item" needs to go
_items[current_position] = item;

}

public T GetFirst()
{
return _items[0];
}

public T Dequeue()
{
int last_position = _items.Count - 1;
T last_item = _items[last_position];
T top = _items[0];
_items[0] = last_item;
_items.RemoveAt(_items.Count - 1);

//percolate down
adjustHeap(0);
return top;
}


private class GenericComparer<TInner> : IComparer<TInner> where TInner : IComparable<TInner>
{
public int Compare(TInner x, TInner y)
{
return x.CompareTo(y);
}
}
}
}
44 changes: 44 additions & 0 deletions src/GitHub.Api/Caching/SerializableCacheItemPolicy.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
/*
Copyright 2012, 2013, 2017 Adam Carter (http://adam-carter.com)

This file is part of FileCache (http://github.com/acarteas/FileCache).

FileCache is distributed under the Apache License 2.0.
Consult "LICENSE.txt" included in this package for the Apache License 2.0.
*/

namespace System.Runtime.Caching
{
[Serializable]
public class SerializableCacheItemPolicy
{
public DateTimeOffset AbsoluteExpiration { get; set; }

private TimeSpan _slidingExpiration;
public TimeSpan SlidingExpiration
{
get
{
return _slidingExpiration;
}
set
{
_slidingExpiration = value;
if (_slidingExpiration > new TimeSpan())
{
AbsoluteExpiration = DateTimeOffset.Now.Add(_slidingExpiration);
}
}
}
public SerializableCacheItemPolicy(CacheItemPolicy policy)
{
AbsoluteExpiration = policy.AbsoluteExpiration;
SlidingExpiration = policy.SlidingExpiration;
}

public SerializableCacheItemPolicy()
{
SlidingExpiration = new TimeSpan();
}
}
}
1 change: 1 addition & 0 deletions src/GitHub.Api/GitHub.Api.csproj
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,6 +17,7 @@
<ItemGroup>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Caching" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading