Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

using cache

  Asked By: Ella    Date: Jul 21    Category: Java    Views: 645
  

I am working on a project which needs to access some files over the internet on
a specific server. Obviously my company has a proxy server setup to get to the
internet. That means in mt application i will need information about.
1. proxy server, port.
2. username and password to access remote server.

I dont want to hard code the values. I can store these values in the database to
make modifications easy. But the problem is that i dont want to hit the database
everytime. I want to be able to cache the details and reuse. At the same time to
avoid stale information, i will have to refresh that cache.

How can I implement it in java? Has anyone ever tried caching certain values? I
dont want to bounce the whole server or get into the code for changing few
parameters.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Julian Long     Answered On: Jul 21

you can build a singleton registry using for example a hashmap, so put
your config values  in registry once and get everytime you need them...

 
Answer #2    Answered By: Omar Walker     Answered On: Jul 21

also handy are properties files  (myvariables.properties). java  supplies
an interface for reading values  out of them, in java.util.Properties

 
Answer #3    Answered By: Bonnie Hughes     Answered On: Jul 21

its prasad architect no need here cache;
i am giving simple example of patten proxy
implementation
please find out
import java.awt.event.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import com.sun.java.swing.border.*;

import java.awt.*;
public class ProxyDisplay extends JxFrame
{
public ProxyDisplay()
{
super("Display proxied image");
JPanel p = new JPanel();
getContentPane().add(p);
p.setLayout(new BorderLayout());
ImageProxy image = new ImageProxy("elliott.jpg",
321, 271);
p.add("Center", image);
p.add("North", new Label(" "));
p.add("West", new Label(" "));
setSize(370, 350);
setVisible(true);
}
//------------------------------------
static public void main(String[] argv)
{
new ProxyDisplay();
}
}
//==================================
class ImageProxy extends JPanel
implements Runnable
{
int height, width;
MediaTracker tracker;
Image img;
JFrame frame;
Thread imageCheck; //to monitor loading
//------------------------------------
public ImageProxy(String filename, int w, int h)
{
height = h;
width = w;

tracker = new MediaTracker(this);
img =
Toolkit.getDefaultToolkit().getImage(filename);
tracker.addImage(img, 0); //watch for image
loading

imageCheck = new Thread(this);
imageCheck.start(); //start 2nd thread
monitor

//this begins actual image loading
try{
tracker.waitForID(0,1);
}
catch(InterruptedException e){}
}
//------------------------------------
public void paint(Graphics g)
{
if (tracker.checkID(0))
{
height = img.getHeight(frame); //get height
width = img.getWidth(frame); //and width

g.setColor(Color.lightGray); //erase box
g.fillRect(0,0, width, height);
g.drawImage(img, 0, 0, this); //draw loaded
image
}
else
{
//draw box outlining image if not loaded yet
g.setColor(Color.black);
g.drawRect(1, 1, width-2, height-2);
}
}
//------------------------------------
public Dimension getPreferredSize()
{
return new Dimension(width, height);
}
//public int getWidth() {return width;}
//public int getHeight(){return height;}
//------------------------------------
public void run()
{
//this thread monitors image loading
//and repaints when done
//the 1000 msec is artifically long
//to allow demo to display with delay
try{
Thread.sleep(1000);
while(! tracker.checkID(0))
Thread.sleep(1000);
}
catch(Exception e){}
repaint();
}
}


class for jxframe

import java.awt.*;
import java.awt.event.*;
import java.util.*;

//swing classes
import com.sun.java.swing.text.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;


public class JxFrame extends JFrame
{
public JxFrame(String title)
{
super(title);
setCloseClick();
setLF();
}
private void setCloseClick()
{
//create window listener to respond to window
close click
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
//------------------------------------------
private void setLF()
{
// Force SwingApp to come up in the System L&F
String laf =
UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
}
catch (UnsupportedLookAndFeelException exc)
{System.err.println("Warning:
UnsupportedLookAndFeel: " + laf);}
catch (Exception exc)
{System.err.println("Error loading " + laf + ": " +
exc);
}
}
}


this is help ful for cache  proxi of perticular file or
image

 
Didn't find what you were looking for? Find more on using cache Or get search suggestion and latest updates.




Tagged: