11 Replies - 557 Views - Last Post: 17 February 2016 - 09:13 AM Rate Topic: -----

#1 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 02:35 PM

I am trying to find the best way to store data received from a socket server I created. I then also need to wait for a client to open the connection to the server and request the data (which will be objects consisting of trade information). After the data is requested the server then forwards a copy to the client in a CSV format.

So I believe I know how to do all of the above but not sure how to cache the data, or if it is wise to use it because of its volatility. I have never used the cache class before and before I dig into it I was curious if I am going in the right direction (in using the cache class)?

If this is not the right direction then please kindly suggest an alternative.

Is This A Good Question/Topic? 0
  • +

Replies To: Is caching the correct class to use for storing data received?

#2 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 02:53 PM

It completely 100% depends on what type of application you're writing, and specifically what "Cache class" you're referring to.

The only one I've seen in the .NET framework that's actually named "Cache" is in the System.Web.Caching namespace. The "Web" part suggests that it's not what you want to use.

But before we even get to "which cache", let's first figure out if you actually need some kind of caching mechanism.

There's a few reasons to cache data:

  • Your application might be closed and re-opened, and you don't want to re-download/re-calculate/re-whatever the information
  • You can't fit your entire data set into memory, so some must be stored in an on-disk cache.


I'm guessing you're not worried about either here.

So, do you really need a cache? Is storing the values in-memory not good enough?

Remember that values you store in your class are persisted in-memory. So if I have a class, and inside that class I have a property that's a List<string>, as long as that class hasn't been garbage collected, everything inside that list is still in memory, still available. It doesn't just vanish.

So you can make your own in-memory cache class. Look into the Dictionary class to actually store your data in.
Was This Post Helpful? 0
  • +
  • -

#3 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 03:08 PM

It appears I was being to literal when I read the instructions. I have no need for a cache, and just could use a simple List.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 03:17 PM

Like I said, check out the Dictionary class. It's like a list, but for "key/value pairs". If you have some kind of identifier that would be a good "key" for each request, you can use that as the key, and the request data as the value. It's extremely efficient at "looking up" values by keys (much faster than looping through the collection to find the item you want).
Was This Post Helpful? 0
  • +
  • -

#5 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 03:27 PM

I was told to use a dictionary at work as well. So if I have a class TposPosition:

public class TposPosition
    {
        public string m_AsOfDate;
        public double m_AvgBuyPrice;
        public double m_AvgCost;
        public double m_AvgSellPrice;
        public string m_ClearingAccount;
        public double m_ContractSize;
        public double m_Coupon;
        public int m_CurQty;
        public double m_DayTradePnL;
        public string m_DisplayFormat;
        public string m_ExpDate;
        public string m_InstrumentID;
        public string m_MarkData;
        public double m_Multiplier;
        public double m_NetPnL;
        public int m_NotionalAmount;
        public double m_OpenMark;
        public int m_OpenQty;
        public string m_Portfolio;
        public string m_PositionDate;
        public string m_PutCall;
        public double m_RealizedPnL;
        public string m_SecType;
        public double m_Strike;
        public string m_Symbol;
        public string m_Ticker;
        public double m_TotalBuyQty;
        public double m_TotalSellQty;
        public string m_TradeDate;
        public string m_TradeGroup;
        public string m_TraderAcronym;
        public string m_UndInstrumentID;
        public string m_UndSecType;
        public string m_UndSymbol;
        public string m_ValidTraderKey;
        public int m_Version;

        public TposPosition();
        public TposPosition(PositionDesc* value);
    }



Correction, I should make a while loop that adds it like this:

Dictionary<int, TposPosition> positions = new Dictionary<int, TposPosition>();


positions.Add(int, TposPosition());

This post has been edited by jphoc13: 16 February 2016 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 16 February 2016 - 03:32 PM

Close. The first generic parameter (object, in your example), is the type of Key you want to use. The second is the type of value.

I don't know what you would like to key your values with. It must be unique (trying to store a second value at the same key will overwrite the first value). Usually it would be something related to the data you're storing.


Edit: looks like you already saw what I was going for there. I'll leave the rest for posterity:

Let's say you have some integer value, called "requestId".

You would make your dictionary like this:

Dictionary<int, TposPosition> positions = new Dictionary<int, TposPosition>();


You could add to it like this:

positions.Add(requestId, position); //requestId: int, position: TposPosition


And retrieve from it like this:

if(positions.ContainsKey(incomingKey)){
  var position = positions[incomingKey];
}

Was This Post Helpful? 1
  • +
  • -

#7 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 08:12 AM

Ok so I need to readress my issue here.

I basically need to store the results of this proxy request into a dictionary, but I am not sure how to do this.

I need to use a dictionary or a hash table because throughoutt the day the "positions" could change, otherwise I would just use a simple list.



Right now I have the program establishing a connection to the server and writing the trades and positions to my console, to make sure the connection works and it does:

 // create a tpos handle and connects to server
l_conn = new TposProxy(l_user, l_pass, l_ip, l_port, l_map);
Console.WriteLine(l_conn.Connect());


This is just my dictionary that I plan to use, key will be an incremented int, and value will be the TposPosition object.

Dictionary<int, TposPosition> positions = new Dictionary<int, TposPosition>();


After I instantiate the dictionary I know I will need a while loop to add it into the dictionary. But I plan on using my int parameter as a for loop counter so that each TPosPosition is assigned a unique key.

so..

while (some reader has next)
{
//a for loop counter that increments the int parameter by 1
positions.Add(int, positions);
}

This post has been edited by jphoc13: 17 February 2016 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 08:22 AM

First question: is each row in the reader a set of data for a different user/request? Or will several rows be part of a single request/response? If it's the latter, you should make one class that will store all the data for a single request/response and use that as the Value parameter of the dictionary.

Second question: is each request/response unique to a user? If so, you could use the username as the Dictionary key instead of some integer. This will make retrieval easier, as you don't have to remember some arbitrary number as the key.

By the way: using a simple incrementing integer as a Dictionary key is kinda pointless. At that point, you might as well use a List<T>, because that has numbered indicies.

But, if you're still going to do it that way: there's no reason you can't declare an integer outside the loop, then increment it in the loop:

var i = 0;
while (someCondition)
{
	//do work here
	i++;
}


Was This Post Helpful? 0
  • +
  • -

#9 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 08:43 AM

"First question: is each row in the reader a set of data for a different user/request? Or will several rows be part of a single request/response? If it's the latter, you should make one class that will store all the data for a single request/response and use that as the Value parameter of the dictionary."

It is the latter, which is why I have the TposPostion object as the value parameter.

If I use the "1_user", my user ID, as the key won't that make each key non-unique?

There is essentially one request, by one user, and that request shows multiple positions (think of positions on stocks). So what I am trying to do is to take each position from that request and have that be my value. The reason I am using a dictionary is because a client will make multiple requests per day to access their positions, because the positions will change as the market changes.

I also have trades, that I need to put into a List, but trades won't change, hence why I am using a List for that one.

I probably didn't describe that properly in the previous posts, sorry, and thank you for your patience.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 09:03 AM

Ah, then your keys should be something that uniquely identifies that position. Perhaps there's a number, string, or GUID that is the "ID" for that position. That should be the key to your dictionary, and how you retrieve data from it.
Was This Post Helpful? 0
  • +
  • -

#11 jphoc13  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 09:08 AM

That is the frustration I am running into, there is not a GUID that uniquely identifies each position, there is absolutely nothing that makes each trade or position unique. That is why I was just assigning one with a for loop. Since the purpose of putting them into a dictionary is to just copy the trades and encode it into a CSV file this shouldn't be an issue.

I just wanted to make sure I wasn't crazy.
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Is caching the correct class to use for storing data received?

Posted 17 February 2016 - 09:13 AM

Well then, if there's nothing that uniquely keys them, there's no reason to use a Dictionary. Just use an array or list, since you would just be using an incrementing integer as the key (that's basically simulating a List's index).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1