473,763 Members | 2,930 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hi, need to create a cache

22 New Member
Hi,
I'm trying to create a cache which will use direct mapping and output a miss rate for a list of integers.
I'm struggling quite a bit.
I think I have created the cache OK, probably not though - heres the code.

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2. import java.util.LinkedHashMap;
  3.  
  4. public class Cache<A,B>
  5. {
  6. public int Size; 
  7. public LinkedHashMap<A,B> Direct;
  8.  
  9.  
  10.  
  11. public Cache(int Size)
  12. {
  13. this.Size = Size;
  14. this.CacheCapacity = CacheCapacity;
  15. this.blocks = blocks;
  16. Direct = new LinkedHashMap<A,B>(Size);
  17.  
  18. }
  19. public B get(A index)
  20. {
  21. return Direct.get(index);
  22. }
  23. public void enter(A index, B Tag)
  24. {
  25. Direct.put(index, Tag);
  26. }
  27. public Collection<Map.Entry<A,B>> getAll() 
  28. {
  29. return new ArrayList<Map.Entry<A,B>>(Direct.entrySet());
  30. }
  31.  
  32.  
  33.  
  34. }
  35.  
I have to have another class, which i call memory, with the test data in.
My cache should be 0.25KB and each block should have 8 bytes.
It's quite confusing really!
Any help would be very appreciated

Thanks

Dave
May 10 '07 #1
6 2524
r035198x
13,262 MVP
Hi,
I'm trying to create a cache which will use direct mapping and output a miss rate for a list of integers.
I'm struggling quite a bit.
I think I have created the cache OK, probably not though - heres the code.

import java.util.*;
import java.util.Linke dHashMap;

public class Cache<A,B>
{
public int Size;
public LinkedHashMap<A ,B> Direct;



public Cache(int Size)
{
this.Size = Size;
this.CacheCapac ity = CacheCapacity;
this.blocks = blocks;
Direct = new LinkedHashMap<A ,B>(Size);

}
public B get(A index)
{
return Direct.get(inde x);
}
public void enter(A index, B Tag)
{
Direct.put(inde x, Tag);
}
public Collection<Map. Entry<A,B>> getAll()
{
return new ArrayList<Map.E ntry<A,B>>(Dire ct.entrySet());
}



}

I have to have another class, which i call memory, with the test data in.
My cache should be 0.25KB and each block should have 8 bytes.
It's quite confusing really!
Any help would be very appreciated

Thanks

Dave
1.) when posting code, please use code tags.
2.) That won't compile. Please try to make it compile first then you can tell us what the problem is.
May 10 '07 #2
ShaveDave27
22 New Member
1.) when posting code, please use code tags.
2.) That won't compile. Please try to make it compile first then you can tell us what the problem is.
Sorry,
heres the code that compiles:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.util.LinkedHashMap;
  3.  
  4. public class Cache<A,B>
  5. {
  6.     public int Size;   
  7.     public LinkedHashMap<A,B> Direct;
  8.  
  9.  
  10.  
  11.     public Cache(int Size)
  12.     {
  13.         this.Size = Size;
  14.  
  15.         Direct = new LinkedHashMap<A,B>(Size);
  16.  
  17.     }
  18.     public B get(A index)
  19.     {
  20.         return Direct.get(index);
  21.     }
  22.     public void enter(A index, B Tag)
  23.     {
  24.         Direct.put(index, Tag);
  25.     }
  26.     public Collection<Map.Entry<A,B>> getAll() 
  27.     {
  28.     return new ArrayList<Map.Entry<A,B>>(Direct.entrySet());
  29.      }
  30.  
  31.  
  32.  
  33. }
  34.  
1) This cache is meant to be able to calculate a miss rate when test data is entered. I can work out the miss rate, but when I input data I can't figure out how to make the cache tell if it's a hit or miss.
2) I need to make the index hold more than one piece of data, at the moment, when a new piece of data is entered, it just replaces the old data.

Thanks

Dave
May 10 '07 #3
r035198x
13,262 MVP
Sorry,
heres the code that compiles:
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.util.LinkedHashMap;
  3.  
  4. public class Cache<A,B>
  5. {
  6. public int Size; 
  7. public LinkedHashMap<A,B> Direct;
  8.  
  9.  
  10.  
  11. public Cache(int Size)
  12. {
  13. this.Size = Size;
  14.  
  15. Direct = new LinkedHashMap<A,B>(Size);
  16.  
  17. }
  18. public B get(A index)
  19. {
  20. return Direct.get(index);
  21. }
  22. public void enter(A index, B Tag)
  23. {
  24. Direct.put(index, Tag);
  25. }
  26. public Collection<Map.Entry<A,B>> getAll() 
  27. {
  28. return new ArrayList<Map.Entry<A,B>>(Direct.entrySet());
  29. }
  30.  
  31.  
  32.  
  33. }
  34.  
1) This cache is meant to be able to calculate a miss rate when test data is entered. I can work out the miss rate, but when I input data I can't figure out how to make the cache tell if it's a hit or miss.
2) I need to make the index hold more than one piece of data, at the moment, when a new piece of data is entered, it just replaces the old data.

Thanks

Dave
Perhaps if you explain what you want to do with an example ...
May 10 '07 #4
ShaveDave27
22 New Member
Perhaps if you explain what you want to do with an example ...
OK,

I have some test data, for example some integers, 22, 33, 22.


These get converted into binary code and then get stored in the cache.
22 = 10110
33=100001
22 = 10110

These binary codes get seperated into an index and a tag. The index is the first two numbers (for all 3 integers this is 10) Then the rest of the code is the tag.

When i input 22, 33, 22 i input it as the index and the tag.
for example I would input 22 as "10" "110".
Then 110 will be stored in index 10.
But when i input 33(also stored in index 10) it will replace 22.
I want them both to be in the index.
And when i input 22 again I want it to realise the same tag has been entered and output "hit", as a variable which can increase everytime there is a hit.

Hope you understand this better

Thanks

Dave
May 10 '07 #5
r035198x
13,262 MVP
OK,

I have some test data, for example some integers, 22, 33, 22.


These get converted into binary code and then get stored in the cache.
22 = 10110
33=100001
22 = 10110

These binary codes get seperated into an index and a tag. The index is the first two numbers (for all 3 integers this is 10) Then the rest of the code is the tag.

When i input 22, 33, 22 i input it as the index and the tag.
for example I would input 22 as "10" "110".
Then 110 will be stored in index 10.
But when i input 33(also stored in index 10) it will replace 22.
I want them both to be in the index.
And when i input 22 again I want it to realise the same tag has been entered and output "hit", as a variable which can increase everytime there is a hit.

Hope you understand this better

Thanks

Dave
Therefore you need the structure

HashMap<Binary, ArrayList<Binar y>> or ofcourse you could simply do away with creating a Binary number class and use
HashMap<String, ArrayList<Strin g>>

Do you see how you can use it?
May 10 '07 #6
mufumbo
3 New Member
Hey,
why don't you use EHCACHE? It's awesome and have all the features that you need, so you don't have to re-invent the wheel.

ehcache.sf.net

Cheers
Rafael
May 12 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

21
2990
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
7
2444
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin Manager. Your main application then hooks into the Plugin Manager. What I'm trying to figure out is how to impliment some form of raising
7
7770
by: moondaddy | last post by:
I want to dynamically create a JavaScript file and cache it on the client for re-use. I know how to write javascript to a web page from the code behind, but I don't know how to actually create a file such as MyNewScript.js and then cache that on the client so all the pages of that session can use it. Can this be done? Thanks -- moondaddy@nospam.com
4
2316
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*, java.text.* " %> <%@ page import="com.plumtree.remote.portlet.*" %> <%@ page import="com.plumtree.remote.prc.*" %> <% String path = request.getContextPath();
2
4456
by: rufpirat | last post by:
Hello I'm in the middle of trying to build an "AD phone book", and this being my first try at asp.net, I have a few questions that I hope some of you might be able to help with: 1. Is it correct, that PageSize equals the max size of the result set? 2. Is there a way to make asp cache the search result, so the domain controller won't be to bother by all the lookups, and also to speed up
5
4482
by: sethwai | last post by:
Hi, I've read everything I can get my hands on and am still very confused about the similarities and differences between db2_mmap_read/write and concurrent i/o. It seems to me at this point that they are virtually identical except that db2_mmap_read/write applies at an instance level and concurrent i/o can be aplied at a tablespace level. It seems that they both bypass the AIX file cache and eliminate i-node locking. Is this correct?
0
3241
by: Phils | last post by:
Hello guys; I i have just found the solution to my problem but would like to full understand the solution so would be great full if you could enlighten me. OUR Set-up
1
1203
by: darrel | last post by:
I've been struggling for some time now getting a RSS app to work. I'm creating RSS from existing XML files (transforming via XSLT). The problem is that the page, itself, is still being sent at UTF-16 when I create the RSS from XSLT. It's UTF-8 when I'm creating the XML myself via the DB and an XMLTextWriter. This gives IE headaches. It adds a space between each character in the XML and doesn't render it as an XML file.
4
1623
by: Girish | last post by:
i have to implement caching in my application, can any one tell me about the good techniques to implement caching, or provide some architectural help , so i can use it to my application. i want to control caching dynamically according to my configuration. Thanks,
0
9383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7364
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3519
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.